zoukankan      html  css  js  c++  java
  • C++ XML文件解析

    使用tinyxml2库,git地址https://github.com/leethomason/tinyxml2

    只需要使用tinyxml2.h tinyxml2.cpp即可,同时需要using namespace tinyxml2

    这里给出从官方test提取出的一些常用的操作

    namespace XMLDemo {
    
    static string fileNames[] = { "./resources/dream.xml",
            "./resources/utf8test.xml", "./resources/empty.xml",
            "./resources/utf8testverify.xml", };
    static void timeTest() {
        XMLDocument* doc = new XMLDocument();
        clock_t startTime = clock();
        doc->LoadFile(fileNames[0].c_str());
        clock_t loadTime = clock();
        int errorID = doc->ErrorID();
        delete doc;
        doc = 0;
        clock_t deleteTime = clock();
    
        printf("Test file '%s' loaded. ErrorID=%d
    ", fileNames[0].c_str(),
                errorID);
        if (!errorID) {
            printf("Load time=%lf sec
    ",
                    (double) (loadTime - startTime) / CLOCKS_PER_SEC);
            printf("Delete time=%lf sec
    ",
                    (double) (deleteTime - loadTime) / CLOCKS_PER_SEC);
            printf("Total time=%lf sec
    ",
                    (double) (deleteTime - startTime) / CLOCKS_PER_SEC);
        }
    }
    static void parseTest() {
        static const char* xml = "<?xml version="1.0"?>"
                "<!DOCTYPE PLAY SYSTEM "play.dtd">"
                "<PLAY>"
                "<TITLE>A Midsummer Night's Dream</TITLE>"
                "</PLAY>";
    
        XMLDocument doc;
        doc.Parse(xml);
    
        XMLElement* titleElement = doc.FirstChildElement("PLAY")->FirstChildElement(
                "TITLE");
        const char* title = titleElement->GetText();
        printf("Name of play (1): %s
    ", title);
    
        XMLText* textNode = titleElement->FirstChild()->ToText();
        title = textNode->Value();
        printf("Name of play (2): %s
    ", title);
    }
    static void valueTest() {
        static const char* xml = "<information>"
                "    <attributeApproach v='2' />"
                "    <textApproach>"
                "        <v>2</v>"
                "    </textApproach>"
                "</information>";
    
        XMLDocument doc;
        doc.Parse(xml);
    
        int v0 = 0;
        int v1 = 0;
    
        XMLElement* attributeApproachElement =
                doc.FirstChildElement()->FirstChildElement("attributeApproach");
        attributeApproachElement->QueryIntAttribute("v", &v0);
    
        XMLElement* textApproachElement =
                doc.FirstChildElement()->FirstChildElement("textApproach");
        textApproachElement->FirstChildElement("v")->QueryIntText(&v1);
    
        printf("Both values are the same: %d and %d
    ", v0, v1);
    }
    static void DOMTest() {
        // Test: Programmatic DOM
        // Build:
        //        <element>
        //            <!--comment-->
        //            <sub attrib="1" />
        //            <sub attrib="2" />
        //            <sub attrib="3" >& Text!</sub>
        //        <element>
    
        XMLDocument* doc = new XMLDocument();
        XMLNode* element = doc->InsertEndChild(doc->NewElement("element"));
    
        XMLElement* sub[3] = { doc->NewElement("sub"), doc->NewElement("sub"),
                doc->NewElement("sub") };
        for (int i = 0; i < 3; ++i) {
            sub[i]->SetAttribute("attrib", i);
        }
        element->InsertEndChild(sub[2]);
        XMLNode* comment = element->InsertFirstChild(doc->NewComment("comment"));
        comment->SetUserData((void*) 2);
        element->InsertAfterChild(comment, sub[0]);
        element->InsertAfterChild(sub[0], sub[1]);
        sub[2]->InsertFirstChild(doc->NewText("& Text!"));
        doc->Print();
        printf("-------------------------------------------------------------
    ");
        // And now deletion:
        element->DeleteChild(sub[2]);
        doc->DeleteNode(comment);
    
        element->FirstChildElement()->SetAttribute("attrib", true);
        element->LastChildElement()->DeleteAttribute("attrib");
        doc->Print();
        printf("-------------------------------------------------------------
    ");
        int value1 = 10;
        int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute(
                "attrib", 10);
        int result =
                doc->FirstChildElement()->LastChildElement()->QueryIntAttribute(
                        "attrib", &value1);
    
        doc->Print();
        printf("-------------------------------------------------------------
    ");
    
        {
            XMLPrinter streamer;
            doc->Print(&streamer);
            printf("%s", streamer.CStr());
        }
        {
            XMLPrinter streamer(0, true);
            doc->Print(&streamer);
        }
        doc->SaveFile("./resources/pretty.xml");
        doc->SaveFile("./resources/compact.xml", true);
        delete doc;
    }
    static void attrTest() {
        const char* str = "<doc/>";
    
        XMLDocument doc;
        doc.Parse(str);
    
        XMLElement* ele = doc.FirstChildElement();
    
        int iVal, iVal2;
        double dVal, dVal2;
    
        ele->SetAttribute("str", "strValue");
        ele->SetAttribute("int", 1);
        ele->SetAttribute("double", -1.0);
    
        const char* cStr = ele->Attribute("str");
        ele->QueryIntAttribute("int", &iVal);
        cout << iVal << endl;
        ele->QueryDoubleAttribute("double", &dVal);
    
        ele->QueryAttribute("int", &iVal2);
        ele->QueryAttribute("double", &dVal2);
        cout << dVal2 << endl;
    
    }
    static void textTest() {
        const char* str = "<foo>This is  text</foo>";
        XMLDocument doc;
        doc.Parse(str);
        XMLElement* element = doc.RootElement();
        cout << element->GetText() << endl;
        element->SetText("abcd");
        cout << element->GetText() << endl;
        doc.Print();
        printf("-------------------------------------------------------------
    ");
    }
    static void printerTest() {
        FILE* printerfp = fopen("resources/printer.xml", "w");
        XMLPrinter printer(printerfp);
        printer.OpenElement("foo");
        printer.PushAttribute("attrib-text", "text");
        printer.PushAttribute("attrib-int", int(1));
        printer.PushAttribute("attrib-unsigned", unsigned(2));
        printer.PushAttribute("attrib-int64", int64_t(3));
        printer.PushAttribute("attrib-bool", true);
        printer.PushAttribute("attrib-double", 4.0);
        printer.CloseElement();
        fclose(printerfp);
    
        XMLDocument doc;
        doc.LoadFile("resources/printer.xml");
    
        const XMLDocument& cdoc = doc;
    
        const XMLAttribute* attrib = cdoc.FirstChildElement("foo")->FindAttribute(
                "attrib-text");
        attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int");
        cout << attrib->Value() << endl;
        attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-unsigned");
        cout << attrib->IntValue() << endl;
        attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool");
        cout << attrib->BoolValue() << endl;
        attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double");
        cout << attrib->DoubleValue() << endl;
    }
    
    
    }
  • 相关阅读:
    SQLServer分组加序号,只取某个对象指定条件的前几个
    SQLServer用with temptb AS临时表查询或者更新字段,将某个字段赋值成某个字段的值
    KMP算法
    java知识点
    程序接口设计的六大原则
    罗马数字转int
    使用github作为maven仓库存放发布自己的jar包依赖 实现多个项目公共部分代码的集中,避免团队中多个项目之间代码的复制粘贴
    java mybatis中大于号小于号的转义
    两个有序数组 A1 A2 的合并
    Mysql_设置root指定的ip访问或连接数据库
  • 原文地址:https://www.cnblogs.com/tla001/p/6710666.html
Copyright © 2011-2022 走看看