转载:http://www.cnblogs.com/1024Planet/p/4401929.html
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<person>
<name>Alan</name>
<age>26</age>
<height>165</height>
<weight>65</weight>
<introduction>C senior engineer</introduction>
</person>
解析代码
#include <stdio.h>
#include "tinyxml.h" int tinyxmlTest(void); int main(int argc, char* argv[])
{
tinyxmlTest();
return ;
} int tinyxmlTest(void)
{
#if (1)
char* xmlStr = "\
<person>\
<name>Alan</name>\
<age></age>\
<height></height>\
<weight></weight>\
<introduction>C senior engineer</introduction>\
</person>"; TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->Parse(xmlStr); #else
TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->LoadFile("person.xml");
#endif
//.....person.....
TiXmlElement* rootElement = myDocument->RootElement();
if (rootElement == NULL || strcmp(rootElement->Value(), "person"))
return ;
printf("%s:\t%s\n", rootElement->Value(), rootElement->GetText()); //.....name.....
TiXmlElement* element = rootElement->FirstChildElement();
if (element == NULL || strcmp(element->Value(), "name"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....age.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "age"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....height.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "height"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....weight.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "weight"))
return ;
printf("%s:\t%s\n", element->Value(), element->GetText()); //.....introduction.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "introduction"))
return ;
printf("%s:\t%s\n\n", element->Value(), element->GetText()); return ;
}
上面是网友贴的代码,我根据实际开发中遇到的问题,贴出自己的代码
第一种:
char* xmlStr = "\<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<data type=\"maplist\">\
<person>\
<name>messi</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
<person>\
<name>kobe</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
<person>\
<name>Alan</name>\
<age></age>\
<height></height>\
<weight></weight>\
</person>\
/data>";
遍历取到每一个person下面节点的name、age、height、weight的value 和text
TiXmlDocument* myDocument ; myDocument.Parse(xmlStr)); TiXmlElement* rootElement = myDocument.RootElement(); if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement(); if (element)
{ for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* AppNameElement = SubItem->ToElement()->FirstChildElement(); TiXmlElement* nameElement = AppNameElement->NextSiblingElement();
TiXmlElement* ageElement = AppPermit_typeElement->NextSiblingElement(); TiXmlElement* heightElement = AppPermit_typeElement->NextSiblingElement(); TiXmlElement* weightElement = AppPermit_typeElement->NextSiblingElement(); printf("%s:\t%s\n", nameElement ->Value(), nameElement ->GetText());
printf("%s:\t%s\n", ageElement ->Value(), ageElement ->GetText());
printf("%s:\t%s\n", heightElement ->Value(), heightElement ->GetText());
printf("%s:\t%s\n", weightElement ->Value(), weightElement ->GetText()); SubItem=SubItem->NextSibling();
}
}
}
第二种:
char* xmlStr = "\<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<data type=\"vector\">\
<Country>USA</Country>\
<Country>China</Country>\
</data>";
TiXmlDocument myDocument; myDocument.Parse(xmlStr); TiXmlElement* rootElement = myDocument.RootElement(); if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement(); if (element)
{
for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* ExtNameElement = SubItem->ToElement(); printf("%s:\t%s\n", NameElement->Value(), NameElement->GetText()); SubItem=SubItem->NextSibling();
}
}
}
}
注:如果你获取的字符串是Unicode的,必须转码为UTF-8