1. 程式人生 > >C++使用TinyXML生成和解析xml檔案

C++使用TinyXML生成和解析xml檔案

TinyXML is a simple, small, C++ XML parser that can be easily integrated into other programs.
官網下載原始碼
下載後把tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp複製到自己的專案中就可以使用了

案例xml檔案

<?xml version="1.0" encoding="UTF-8" ?>
<document>
    <English name
="name1" value="value">
The world has many languages</English> <Russian name="название(имя)" value="ценность">Мир имеет много языков</Russian> <Heavy>changshuhang;</Heavy> <csh>Umlaut Element</csh> <phonebook> <!--one item behalfs one contacted person.-->
<item> <name>miaomaio</name> <addr>Shaanxi Xi&apos;an</addr> <tel>13759911917</tel> <email>[email protected]</email> </item> <item> <name>gougou</name
>
<addr>Liaoning Shenyang</addr> <tel>15840330481</tel> <email>[email protected]</email> </item> <!--more contacted persons.--> </phonebook> </document>

解析程式

#include <iostream>

#include "tinyxml.h" 

#define log(x,y) std::cout <<"\""<< x <<"\" = " << y << std::endl

using namespace std;

void read(TiXmlDocument document);
void save();

void main()
{
    cout << "Hello TinyXML" << endl;

    TiXmlDocument document("e://demo.xml");
    document.LoadFile();

    //read(document);
    save();

    ////////////////////////////////////////////////////////////////////////////////
    // Loop until user enters q or Q
    ////////////////////////////////////////////////////////////////////////////////
    char c(' ');

    while (c != 'q' && c != 'Q')
    {
        cout << "Press q then enter to quit: ";
        cin >> c;
    }
}

//讀取資料
void read(TiXmlDocument document)
{
    TiXmlElement* root = document.FirstChildElement("document");

    //讀取English
    TiXmlElement* English = root->FirstChildElement("English");
    TiXmlAttribute* EnglishAttribute = English->FirstAttribute();
    for (; EnglishAttribute; EnglishAttribute = EnglishAttribute->Next())
    {
        log(EnglishAttribute->Name(), EnglishAttribute->Value());
    }
    log("English", English->GetText());

    //讀取English
    TiXmlElement* Russian = root->FirstChildElement("Russian");
    TiXmlAttribute* RussianAttribute = English->FirstAttribute();
    for (; RussianAttribute; RussianAttribute = RussianAttribute->Next())
    {
        log(RussianAttribute->Name(), RussianAttribute->Value());
    }
    log("Russian", Russian->GetText());

    //讀取item
    TiXmlElement* item = root->FirstChildElement("phonebook")->FirstChildElement("item");
    for (; item; item = item->NextSiblingElement())
    {
        TiXmlElement* name = item->FirstChildElement("name");
        log("name", name->GetText());

        name = item->FirstChildElement("addr");
        log("addr", name->GetText());

        name = item->FirstChildElement("tel");
        log("tel", name->GetText());

        name = item->FirstChildElement("email");
        log("email", name->GetText());
    }
}

//儲存資訊
void save()
{
    TiXmlDocument document("e://demo1.xml");
    document.LoadFile();

    TiXmlElement* root = document.RootElement();

    const TiXmlElement* English = new TiXmlElement("English");
    TiXmlAttributeSet * EnglishArray = new TiXmlAttributeSet();
    TiXmlAttribute * a1 = new TiXmlAttribute("name", "changshuhang");
    EnglishArray->Add(a1);
    TiXmlAttribute * a2 = new TiXmlAttribute("value", "Shaanxi Xi&apos;an");
    EnglishArray->Add(a2);
    const char * name = "name";
    const char * value = "changshuhang";
    //English->SetAttribute(name, value);//該行資料報錯
    root->InsertEndChild(*English)->InsertEndChild(TiXmlText("The world has many languages"));

    document.SaveFile();
}

哪位大神知道怎樣設定屬性,小弟根據官方文件老是報錯,而且也沒有找到設定TiXmlAttributeSet的方法,求指導一下!