tinyxml性能太差了,小文件还行,大文件痛苦死了 pugixml是一个不错的选择
一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串。
XML文件
代码语言:javascript复制<?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>
代码语言:javascript复制#include <stdio.h>
#include "tinyxml.h"
int tinyxmlTest(void);
int main(int argc, char* argv[])
{
tinyxmlTest();
return 1;
}
int tinyxmlTest(void)
{
#if (1)
char* xmlStr = "
<person>
<name>Alan</name>
<age>26</age>
<height>165</height>
<weight>65</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 0;
printf("%s:t%sn", rootElement->Value(), rootElement->GetText());
//.....name.....
TiXmlElement* element = rootElement->FirstChildElement();
if (element == NULL || strcmp(element->Value(), "name"))
return 0;
printf("%s:t%sn", element->Value(), element->GetText());
//.....age.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "age"))
return 0;
printf("%s:t%sn", element->Value(), element->GetText());
//.....height.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "height"))
return 0;
printf("%s:t%sn", element->Value(), element->GetText());
//.....weight.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "weight"))
return 0;
printf("%s:t%sn", element->Value(), element->GetText());
//.....introduction.....
element = element->NextSiblingElement();
if (element == NULL || strcmp(element->Value(), "introduction"))
return 0;
printf("%s:t%snn", element->Value(), element->GetText());
return 1;
}