TinyXML经典入门
发布时间:2020-12-16 02:19:15 所属栏目:百科 来源:网络整理
导读:1.TinyXML 1.1.简介 TinyXML2.0是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM(Document Object Model)模型,从而让我们很方便的遍历这棵XML树。 下载网址 https://github
1.TinyXML1.1.简介TinyXML2.0是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM(Document Object Model)模型,从而让我们很方便的遍历这棵XML树。
1.2.Qt+TinyXML环境搭建下载完成后,解压找到tinyxml2.cpp 以及tinyxml2.h,并且添加到c++工程目录当中,与其他源码一起编译即可。 #include <iostream>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std;
int main()
{
XMLDocument doc;
doc.LoadFile("baidu.xml");
doc.Print();
cout<<"doc status "<<doc.ErrorName()<<endl;
return 0;
}
1.3.读写XML1.3.1.写#include <iostream>
#include <string.h>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std;
#if 0
<?xml version="1.0" encoding="utf-8"?>
<<!-- This is a XML comment --> >
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
;
#endif
int main()
{
XMLDocument *pDoc=new XMLDocument; //定义一个文档的指针
XMLDeclaration *pDel = pDoc->NewDeclaration("xml version="1.0" encoding="UTF-8"");
pDoc->LinkEndChild(pDel);
XMLComment *pCom = pDoc->NewComment("This is a XML comment");
pDoc->LinkEndChild(pCom);
XMLElement *pEleRootBookStore = pDoc->NewElement("bookstore");
XMLElement *pEleBookOfBookStore = pDoc->NewElement("book");
pEleBookOfBookStore->SetAttribute("category","COOKING");
XMLElement *pEleTitleOfBook = pDoc->NewElement("title");
pEleTitleOfBook->SetAttribute("lang","en");
pEleTitleOfBook->SetText("Everyday Italian");
pEleBookOfBookStore->LinkEndChild(pEleTitleOfBook);
XMLElement *pEleAuthorOfBook = pDoc->NewElement("author");
pEleAuthorOfBook->SetText("Giada De Laurentiis");
pEleBookOfBookStore->LinkEndChild(pEleAuthorOfBook);
XMLElement *pEleYearOfBook = pDoc->NewElement("year");
pEleYearOfBook->SetText(2005);
pEleBookOfBookStore->LinkEndChild(pEleYearOfBook);
XMLElement *pElePriceOfBook = pDoc->NewElement("price");
pElePriceOfBook->SetText(30.00);
pElePriceOfBook->LinkEndChild(pElePriceOfBook);
pEleRootBookStore->LinkEndChild(pEleBookOfBookStore);
pDoc->LinkEndChild(pEleRootBookStore);
pDoc->Print();
pDoc->SaveFile("bookStore.xml");
free(pDoc);
return 0;
}
1.3.2.读#include <iostream>
#include <string.h>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std;
#if 0
<?xml version="1.0" encoding="utf-8"?>
<<!-- This is a XML comment --> >
<bookstore>
<book category = "COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
;
#endif
int main()
{
XMLDocument *pDoc = new XMLDocument;
pDoc->LoadFile("bookStore.xml");
XMLElement *pRoot = pDoc->RootElement();
cout<<"rootEleName :"<<pRoot->Name()<<endl;
XMLElement *pEle = pRoot->FirstChildElement();
cout<<"FirstChildName:"<<pEle->Name()<<endl;
const XMLAttribute *pEleAttr = pEle->FirstAttribute();
cout<<"FirstChildAttrName:"<<pEleAttr->Name()<<" AttrValue:"<<pEleAttr->Value()<<endl;
pEle->SetAttribute("category","STUDY");
pEle = pEle->FirstChildElement();
cout<<" Name:"<<pEle->Name()
<<" AttrName:"<<pEle->FirstAttribute()->Name()
<<" AttrValue:"<<pEle->FirstAttribute()->Value()
<<" Text:"<<pEle->GetText()<<endl;
pEle = pEle->NextSiblingElement();
cout<<" Name:"<<pEle->Name()
<<" Text:"<<pEle->GetText()<<endl;
pEle = pEle->NextSiblingElement();
cout<<" Name:"<<pEle->Name()
<<" Text:"<<pEle->GetText()<<endl;
XMLElement *ele = pDoc->NewElement("Water");
ele->SetAttribute("aa","bb");
ele->SetText("good Water");
pEle = pDoc->FirstChildElement();
pEle->InsertEndChild(ele);
pDoc->Print();
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |