使用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[].c_str());
clock_t loadTime = clock();
int errorID = doc->ErrorID();
delete doc;
doc = ;
clock_t deleteTime = clock(); printf("Test file '%s' loaded. ErrorID=%d\n", fileNames[].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 = ;
int v1 = ; 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[] = { doc->NewElement("sub"), doc->NewElement("sub"),
doc->NewElement("sub") };
for (int i = ; i < ; ++i) {
sub[i]->SetAttribute("attrib", i);
}
element->InsertEndChild(sub[]);
XMLNode* comment = element->InsertFirstChild(doc->NewComment("comment"));
comment->SetUserData((void*) );
element->InsertAfterChild(comment, sub[]);
element->InsertAfterChild(sub[], sub[]);
sub[]->InsertFirstChild(doc->NewText("& Text!"));
doc->Print();
printf("-------------------------------------------------------------\n");
// And now deletion:
element->DeleteChild(sub[]);
doc->DeleteNode(comment); element->FirstChildElement()->SetAttribute("attrib", true);
element->LastChildElement()->DeleteAttribute("attrib");
doc->Print();
printf("-------------------------------------------------------------\n");
int value1 = ;
int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute(
"attrib", );
int result =
doc->FirstChildElement()->LastChildElement()->QueryIntAttribute(
"attrib", &value1); doc->Print();
printf("-------------------------------------------------------------\n"); {
XMLPrinter streamer;
doc->Print(&streamer);
printf("%s", streamer.CStr());
}
{
XMLPrinter streamer(, 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", );
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());
printer.PushAttribute("attrib-unsigned", unsigned());
printer.PushAttribute("attrib-int64", int64_t());
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;
} }

C++ XML文件解析的更多相关文章

  1. 通过正则表达式实现简单xml文件解析

    这是我通过正则表达式实现的xml文件解析工具,有些XHTML文件中包含特殊符号,暂时还无法正常使用. 设计思路:常见的xml文件都是单根树结构,工具的目的是通过递归的方式将整个文档树装载进一个Node ...

  2. 八、Android学习第七天——XML文件解析方法(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 八.Android学习第七天——XML文件解析方法 XML文件:exten ...

  3. android基础知识13:AndroidManifest.xml文件解析

    注:本文转载于:http://blog.csdn.net/xianming01/article/details/7526987 AndroidManifest.xml文件解析. 1.重要性 Andro ...

  4. Android之AndroidManifest.xml文件解析

    转自:Android学习笔记之AndroidManifest.xml文件解析 一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文 ...

  5. 9.XML文件解析

    一.XML简介 XML(EXtensible Markup Language),可扩展标记语言 特点:XML与操作系统.编程语言的开发平台无关 实现不同系统之间的数据交换 作用:数据交互 配置应用程序 ...

  6. Python实现XML文件解析

    1. XML简介 XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用.它是web ...

  7. Python3将xml文件解析为Python对象

    一.说明 从最开始写javascript开始,我就很烦感使用getElementById()等函数来获取节点的方法,获取了一个节点要访问其子孙节点要么child半天要么就再来一个getElementB ...

  8. XML文件解析-DOM4J方式和SAX方式

    最近遇到的工作内容都是和xml内容解析相关的. 1图片数据以base64编码的方式保存在xml的一个标签中,xml文件通过接口的方式发送给我,然后我去解析出图片数据,对图片进行进一步处理. 2.xml ...

  9. java基础之概谈xml文件解析

    XML已经成为一种非常通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便. 诸多web应用框架,其可配置的编程方式,给我们的开发带来了非常大程度的便捷,但细细 ...

  10. XML文件解析之JDOM解析

    1.JDOM介绍 JDOM的官方网站是http://www.jdom.org/,JDOM解析用到的jar包可以在http://www.jdom.org/dist/binary/中下载,最新的JDOM2 ...

随机推荐

  1. 分布式压测系列之Jmeter4.0第一季

    1)Jmeter4.0介绍 jmeter是个纯java编写的开源压测工具,apache旗下的开源软件,一开始是设计为web测试的软件,由于发展迅猛,现在可以压测许多协议比如:http.https.so ...

  2. [ZJOI2006]超级麻将(动规)

    题目描述 很多人都知道玩麻将,当然也有人不知道,呵呵,不要紧,我在这里简要地介绍一下麻将规则: 普通麻将有砣.索.万三种类型的牌,每种牌有1~9个数字,其中相同的牌每个有四张,例如1砣~9砣,1索~9 ...

  3. 2018 Wannafly summer camp Day3--Shopping

    Shopping 描述 题目描述: 你要买n件物品,其中有一些是凳子. 商场正在举行促销活动,如果购物车中有至少一个凳子,那么你可以半价购买这个购物车中最贵的一个物品. 你有m辆购物车,请最小化你的花 ...

  4. pt-online-schema-change 脚本化

    mysql在线更改表可用工具 pt-online-schema-change 更改,或者用gh-ost更改.pt-online-schema-change 在原表创建索引,跟踪新插入的数据.gh-os ...

  5. checked="checked"无效,radio未选中问题排查

    在使用attr给元素设置属性的时候,但是显示在页面就是未选中,而且是只使用一两次是ok的,但是连着多使用就不起作用了,百度了下,发现遇到这个问题的人还蛮多的, 有人说,发现在jQuery1.6版本之后 ...

  6. Post 和 Get的区别?

    Post方法: 1. POST 请求的数据不会被缓存 2. Post请求的内容放置在HTML header中,用户是看不到这个过程的.所以是比较安全的 3. Post请求的数据大小没有限制 Get方法 ...

  7. 中间件kafka

    * kafka----一个发布订阅消息系统,中间件:一个分布式.分区.可重复的日志服务kafka需要了解基础几层结构,生产者订阅者等使用方法,和在高并发.一致性场景使用.(凡事面试问一致性.高并发都脱 ...

  8. apache使用.htaccess文件中RewriteRule重定向后,URL中的加号无法解析

    今天在使用.htaccess做伪静态的时候,发生一件怪事,URL里存在C++时会有问题,在处理C++这个词的时候,无论如何,$_GET都得不到++,只能得到C空格. 一开始我以为是没用urlencod ...

  9. PHP (Yii2) 自定义业务异常类(可支持返回任意自己想要的类型数据)

    public function beforeAction($action) { return parent::beforeAction($action); } public function runA ...

  10. Excel学习路径总结

    本片涉及从入门到Excel的各个方向,包含众多资料和自己学习的心得,希望您可以仔细阅之:   入门篇: 无论是软件,还是编程,最好的入门就是通过看视频来学习,视频优点为很容易看清楚,手把手教授,不容易 ...