使用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. VS2012 Getting Started with Owin and Katana

    参考地址:http://www.asp.net/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana 小提示: 该示 ...

  2. code First 三 Fluent API

    Entity Framework Fluent API用于配置域类以覆盖约定. 在实体框架6中,DbModelBuilder类充当Fluent API,我们可以使用它来配置许多不同的东西.它提供了比数 ...

  3. vue2 自定义全局组件(Loading加载效果)

    vue2 自定义全局组件(Loading加载效果) github地址: https://github.com/ccyinghua/custom-global-component 一.构建项目 vue ...

  4. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

  5. N个数求和

    题目: 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式. 输入格式: 输入第一行给出一个正整数N(≤100).随后一行按格式a ...

  6. (第01节)IDEA快速搭建web项目

    在配置好环境,熟悉了IDEA的基本操作后,就要开始搭建WEB项目了: File——>new——>project——>然后选择Maven 点击Create from archetype ...

  7. Mit6.824 Lab1-MapReduce

    前言 Mit6.824 是我在学习一些分布式系统方面的知识的时候偶然看到的,然后就开始尝试跟课.不得不说,国外的课程难度是真的大,一周的时间居然要学一门 Go 语言,然后还要读论文,进而做MapRed ...

  8. Linux下安装google拼音输入法

    首先安装fcitx,前几天看了很多在ubuntu上能够使用的输入法,有人推荐是搜狗输入法,毕竟是国产嘛,但是会有意外发生,比如说安装之后会产生输入的字符乱码,是一堆看不懂的东西,我就是因为遇到了,然后 ...

  9. bootstrap Table动态绑定数据并自定义字段显示值

    第一步:我们在官网下载了bootstrap 的文档,并在项目中引入bootstrap table相关js文件,当然,也要记得引入jquery文件 大概如图: 第二步:定义一个table控件 第三步:j ...

  10. js实现监听浏览器窗口大小改变事件

    window.onresize = function(){   }