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的方法,求指导一下!

你可能感兴趣的:(C++,xml,tinyxml)