C++ XML文件解析

{

本文来自  https://www.cnblogs.com/tla001/p/6710666.html

}

{

 

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\n", fileNames[0].c_str(),
            errorID);
    if (!errorID) {
        printf("Load time=%lf sec\n",
                (double) (loadTime - startTime) / CLOCKS_PER_SEC);
        printf("Delete time=%lf sec\n",
                (double) (deleteTime - loadTime) / CLOCKS_PER_SEC);
        printf("Total time=%lf sec\n",
                (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\n", title);

    XMLText* textNode = titleElement->FirstChild()->ToText();
    title = textNode->Value();
    printf("Name of play (2): %s\n", 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\n", 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("-------------------------------------------------------------\n");
    // And now deletion:
    element->DeleteChild(sub[2]);
    doc->DeleteNode(comment);

    element->FirstChildElement()->SetAttribute("attrib", true);
    element->LastChildElement()->DeleteAttribute("attrib");
    doc->Print();
    printf("-------------------------------------------------------------\n");
    int value1 = 10;
    int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute(
            "attrib", 10);
    int result =
            doc->FirstChildElement()->LastChildElement()->QueryIntAttribute(
                    "attrib", &value1);

    doc->Print();
    printf("-------------------------------------------------------------\n");

    {
        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("-------------------------------------------------------------\n");
}
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;
}

}

 

}

原文链接: https://www.cnblogs.com/YZFHKMS-X/p/12730587.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ XML文件解析

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/343037

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午1:55
下一篇 2023年3月2日 上午1:55

相关推荐