开发者

tinyxml how to work with it when multiple xml files?

i have an xml like this

<xml http://......>
<value>
<name>me</name>
<age>12</age>
</value>
<value&开发者_运维技巧gt;
<name>kk</name>
<age>1</age>
</value>
</xml>

this xml is in a string value called s; i did:

const char *data =s.c_str();

TiXmlDocument doc;
doc.Parse((const char*)data, 0, TIXML_ENCODING_UTF8);
const std::string m_name;

TiXmlHandle handle(&doc);
TiXmlElement* section;
section = handle.FirstChild("xml").FirstChild("value").FirstChild("name").Element();
if (section) {//code }

it gives me just the name from the first . How to go to the second ?

thx


Use NextSibling or NextSiblingElement to get to the next element on the same hierarchy of the current subtree of the DOM.

All siblings are linked together and calling NextSibling on the last of them will return NULL. Assuming you have the first child element and want to run some code on it and on all of its siblings, it could looks like this:

TiXmlElement* element = ... (first child element)
do {
    // process the current element
}
// try to advance to the next sibling, break the loop if there is none.
while((element = element->NextSiblingElement()) != NULL);


Try this one instead of the last 3 lines of your code:

TiXmlElement* xml = handle.FirstChildElement("xml");

TiXmlElement* value = xml->FirstChildElement("value");
while (value)
{
    TiXmlElement* section = value->FirstChildElement("name");
    if (section) 
    {
        //code 
    }
    value = value->NextSiblingElement("value");
}

Actually you should check, if the results of value->FirstChildElement("name") and handle->FirstChildElement("xml") are not NULL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜