在c ++中使用tiny xml进行xml解析的问题?

时间:2013-09-23 12:01:27

标签: c++ xml xml-parsing tinyxml

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

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

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>

1 个答案:

答案 0 :(得分:1)

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

<node>This is text!</node>

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

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

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

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

产生“testAPPlication”。

一般情况下,请先查看TinyXML的full documentation

编辑:以下示例以简单而又丑陋的方式描述了您的需求:

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 :删除了无限循环和条件。