小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

TinyXML(解析XML,支持c++ )

 zhushasha520 2011-08-09
 

TinyXML是一個開源的解析XML的解析庫,能夠用于C++,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML文件,然后在內(nèi)存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。

DOM模型即文檔對象模型,是將整個文檔分成多個元素(如書、章、節(jié)、段等),并利用樹型結(jié)構(gòu)表示這些元素之間的順序關(guān)系以及嵌套包含關(guān)系。

如下是一個XML片段:
   <Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
        <Person ID="2">
            <name>白晶晶</name>
            <age>18</age>
        </Person>
    </Persons>

 在TinyXML中,根據(jù)XML的各種元素來定義了一些類:

TiXmlBase:整個TinyXML模型的基類。

TiXmlAttribute:對應(yīng)于XML中的元素的屬性。

TiXmlNode:對應(yīng)于DOM結(jié)構(gòu)中的節(jié)點。

TiXmlComment:對應(yīng)于XML中的注釋

TiXmlDeclaration:對應(yīng)于XML中的申明部分,即<?versiong="1.0" ?>。

TiXmlDocument:對應(yīng)于XML的整個文檔。

TiXmlElement:對應(yīng)于XML的元素。

TiXmlText:對應(yīng)于XML的文字部分

TiXmlUnknown:對應(yīng)于XML的未知部分。 

TiXmlHandler:定義了針對XML的一些操作。

TinyXML是個解析庫,主要由DOM模型類(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作類(TiXmlHandler)構(gòu)成。它由兩個頭文件(.h文件)和四個CPP文件(.cpp文件)構(gòu)成,用的時候,只要將(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)導(dǎo)入工程就可以用它的東西了。如果需要,可以將它做成自己的DLL來調(diào)用。舉個例子就可以說明一切。。。

對應(yīng)的XML文件:
<Persons>
    <Person ID="1">
        <name>phinecos</name>
        <age>22</age>
    </Person>
</Persons>

讀寫XML文件的程序代碼:

#include <iostream>
#include "tinyxml.h"
#include "tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace std;

CString GetAppPath()
{//獲取應(yīng)用程序根目錄
    TCHAR modulePath[MAX_PATH];
    GetModuleFileName(NULL, modulePath, MAX_PATH);
    CString strModulePath(modulePath);
    strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
    return strModulePath;
}

bool CreateXmlFile(string& szFileName)
{//創(chuàng)建xml文件,szFilePath為文件保存的路徑,若創(chuàng)建成功返回true,否則false
    try
    {
        //創(chuàng)建一個XML的文檔對象。
        TiXmlDocument *myDocument = new TiXmlDocument();
        //創(chuàng)建一個根元素并連接。
        TiXmlElement *RootElement = new TiXmlElement("Persons");
        myDocument->LinkEndChild(RootElement);
        //創(chuàng)建一個Person元素并連接。
        TiXmlElement *PersonElement = new TiXmlElement("Person");
        RootElement->LinkEndChild(PersonElement);
        //設(shè)置Person元素的屬性。
        PersonElement->SetAttribute("ID", "1");
        //創(chuàng)建name元素、age元素并連接。
        TiXmlElement *NameElement = new TiXmlElement("name");
        TiXmlElement *AgeElement = new TiXmlElement("age");
        PersonElement->LinkEndChild(NameElement);
        PersonElement->LinkEndChild(AgeElement);
        //設(shè)置name元素和age元素的內(nèi)容并連接。
        TiXmlText *NameContent = new TiXmlText("周星星");
        TiXmlText *AgeContent = new TiXmlText("22");
        NameElement->LinkEndChild(NameContent);
        AgeElement->LinkEndChild(AgeContent);
        CString appPath = GetAppPath();
        string seperator = "\\";
        string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
        myDocument->SaveFile(fullPath.c_str());//保存到文件
    }
    catch (string& e)
    {
        return false;
    }
    return true;
}

bool ReadXmlFile(string& szFileName)
{//讀取Xml文件,并遍歷
    try
    {
        CString appPath = GetAppPath();
        string seperator = "\\";
        string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
        //創(chuàng)建一個XML的文檔對象。
        TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
        myDocument->LoadFile();
        //獲得根元素,即Persons。
        TiXmlElement *RootElement = myDocument->RootElement();
        //輸出根元素名稱,即輸出Persons。
        cout << RootElement->Value() << endl;
        //獲得第一個Person節(jié)點。
        TiXmlElement *FirstPerson = RootElement->FirstChildElement();
        //獲得第一個Person的name節(jié)點和age節(jié)點和ID屬性。
        TiXmlElement *NameElement = FirstPerson->FirstChildElement();
        TiXmlElement *AgeElement = NameElement->NextSiblingElement();
        TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
        //輸出第一個Person的name內(nèi)容,即周星星;age內(nèi)容,即;ID屬性,即。
        cout << NameElement->FirstChild()->Value() << endl;
        cout << AgeElement->FirstChild()->Value() << endl;
        cout << IDAttribute->Value()<< endl;
    }
    catch (string& e)
    {
        return false;
    }
    return true;
}
int main()
{
    string fileName = "info.xml";
    CreateXmlFile(fileName);
    ReadXmlFile(fileName);
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多