0

我正在尝试使用 tiny xml 解析器解析以下 xml。我正在尝试获取 xml 文件中 q2channel 标记的所有生产者和消费者属性的值。我正在使用 tinyxml2。

到目前为止,我已经编写了以下代码,但我被困在这里,因为当我尝试打印一些我得到空的东西时。

XMLDocument doc;
    bool loadOkay=doc.LoadFile( "resources/q2profiling.xml" );


    XMLElement* titleElement = doc.FirstChildElement()->FirstChildElement();
    const char* title = titleElement->GetText();
    printf( "The remaining xml is %s\n", title );

我已经尝试了几种不同的代码变体来推进我的工作,但我被困在这里。请帮助我从 xml 打印 q2channel 标记的所有消费者和生产者属性的值。

xml文件如下所示

<

?xml version="1.0" encoding="UTF-8" ?>
<q2:profiles xmlns:q2="http://www.example.org/q2profiling" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/q2profiling q2profiling.xsd">
    <q2:application name="testAPPlication">
        <q2:QDUGraph>
            <q2:channel producer="UNKNOWN_PRODUCER(CONSTANT_DATA)" consumer="Out_of_the_main_function_scope">
                <q2:UnMA>19298</q2:UnMA>
                <q2:Bytes>25892</q2:Bytes>
                <q2:UnDV>19298</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134512692" upper="134512695" />
                    <q2:range lower="134512700" upper="134512703" />        
                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="UNKNOWN_PRODUCER(CONSTANT_DATA)" consumer="main">
                <q2:UnMA>160</q2:UnMA>
                <q2:Bytes>234</q2:Bytes>
                <q2:UnDV>160</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134513084" upper="134513087" />
                    <q2:range lower="134513116" upper="134513119" />
                    <q2:range lower="134513129" upper="134513129" />
                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="Out_of_the_main_function_scope" consumer="Out_of_the_main_function_scope">
                <q2:UnMA>1474</q2:UnMA>
                <q2:Bytes>14815</q2:Bytes>
                <q2:UnDV>2468</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520828" upper="134520831" />
                    <q2:range lower="134520836" upper="134520843" />
                    <q2:range lower="3077693172" upper="3077693175" />

                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="Out_of_the_main_function_scope" consumer="main">
                <q2:UnMA>209</q2:UnMA>
                <q2:Bytes>381</q2:Bytes>
                <q2:UnDV>209</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520836" upper="134520847" />
                    <q2:range lower="3045611784" upper="3045611795" />

                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="main" consumer="Out_of_the_main_function_scope">
                <q2:UnMA>40</q2:UnMA>
                <q2:Bytes>60</q2:Bytes>
                <q2:UnDV>40</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="3048639008" upper="3048639019" />
                    <q2:range lower="3048639024" upper="3048639031" />

                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="main" consumer="main">
                <q2:UnMA>32</q2:UnMA>
                <q2:Bytes>444</q2:Bytes>
                <q2:UnDV>88</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="3048639008" upper="3048639011" />
                    <q2:range lower="3048639016" upper="3048639019" />

                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="UNKNOWN_PRODUCER(CONSTANT_DATA)" consumer="sum">
                <q2:UnMA>1</q2:UnMA>
                <q2:Bytes>1000</q2:Bytes>
                <q2:UnDV>1</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520860" upper="134520860" />
                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="UNKNOWN_PRODUCER(CONSTANT_DATA)" consumer="diff">
                <q2:UnMA>1</q2:UnMA>
                <q2:Bytes>1000</q2:Bytes>
                <q2:UnDV>1</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520860" upper="134520860" />
                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="init" consumer="sum">
                <q2:UnMA>2000</q2:UnMA>
                <q2:Bytes>2000</q2:Bytes>
                <q2:UnDV>2000</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520896" upper="134521895" />
                    <q2:range lower="134523968" upper="134524967" />
                </q2:UnMARanges>
            </q2:channel>
            <q2:channel producer="init" consumer="diff">
                <q2:UnMA>2000</q2:UnMA>
                <q2:Bytes>2000</q2:Bytes>
                <q2:UnDV>2000</q2:UnDV>
                <q2:UnMARanges>
                    <q2:range lower="134520896" upper="134521895" />
                    <q2:range lower="134523968" upper="134524967" />
                </q2:UnMARanges>
            </q2:channel>
        </q2:QDUGraph>
    </q2:application>
</q2:profiles>
4

1 回答 1

1

该函数GetText()不会为您提供相关节点的实际名称,而是节点内的文本。例如,GetText()在这个假设节点上调用

<node>This is text!</node>

将产生“这是文本!” 作为 aconst char*或 aconst std::string&取决于您的编译方式。请参阅TiXmlElement的 API 参考。

如果要查询节点名称本身,请使用TiXmlNode::Value().

如果你想要一个属性,只需在元素上使用Attribute(const char*)或。QueryStringAttribute(...)例如,使用您提供的文档检索并打印以下内容

std::cout << doc.FirstChildElement ()->FirstChildElement ()->Attribute ("name") << std::endl;

产生“testAPPlication”。

一般来说,首先查看 TinyXML 的完整文档

编辑:以下示例以简单但丑陋的方式描述了您需要的内容:

TiXmlDocument doc;

if(doc.LoadFile ("test.xml"))
{
  TiXmlElement* graphElem   = doc.FirstChildElement()->FirstChildElement ()->FirstChildElement ();
  TiXmlElement* channelElem = graphElem->FirstChildElement ();

  for(; channelElem; channelElem = channelElem->NextSiblingElement ())
  {
    std::cout << channelElem->Attribute ("producer")
              << " | "
              << channelElem->Attribute ("consumer")
              << std::endl;
  }
}

EDIT2:删除了无限循环和条件。

于 2013-09-23T12:35:24.900 回答