TinyXML经典入门

1.TinyXML

1.1.简介

TinyXML2.0是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM(Document Object Model)模型,从而让我们很方便的遍历这棵XML树。
下载网址

https://github.com/leethomason/tinyxml2

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.读写XML

1.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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

developer_wgl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值