您的位置:首页 > 编程语言 > C语言/C++

C++ XML文件解析

2017-04-14 20:04 183 查看
使用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\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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: