tinyXml直接解析XML字符串

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/vv1025/article/details/102767447

tinyxml性能太差了,小文件还行,大文件痛苦死了
pugixml是一个不错的选择

一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串。

XML文件

<?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 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%s\n", rootElement->Value(), rootElement->GetText());

    //.....name.....
  TiXmlElement* element = rootElement->FirstChildElement();
    if (element == NULL || strcmp(element->Value(), "name"))
        return 0;
    printf("%s:\t%s\n", element->Value(), element->GetText());

    //.....age.....
    element = element->NextSiblingElement();
    if (element == NULL || strcmp(element->Value(), "age"))
        return 0;
    printf("%s:\t%s\n", element->Value(), element->GetText());
    
    //.....height.....
    element = element->NextSiblingElement();
    if (element == NULL || strcmp(element->Value(), "height"))
        return 0;
    printf("%s:\t%s\n", element->Value(), element->GetText());

    //.....weight.....
    element = element->NextSiblingElement();
    if (element == NULL || strcmp(element->Value(), "weight"))
        return 0;
    printf("%s:\t%s\n", element->Value(), element->GetText());

    //.....introduction.....
    element = element->NextSiblingElement();
    if (element == NULL || strcmp(element->Value(), "introduction"))
        return 0;
    printf("%s:\t%s\n\n", element->Value(), element->GetText());

    return 1;
}

猜你喜欢

转载自blog.csdn.net/vv1025/article/details/102767447