毕业论文中使用的XML读取和制造工具!!!C++ 使用TinyXML解析XML文件

1.介绍

  读取和设置xml配置文件是最常用的操作,TinyXML是一个开源的解析XML的C++解析库,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。  

  下载TinyXML的网址:http://www.grinninglizard.com/tinyxml/

  使用TinyXML只需要将其中的6个文件拷贝到项目中就可以直接使用了,这六个文件是:

tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。

2.读取XML文件

如读取文件a.xml:

<Todo>
    <Item priority="1"> 
        <bold>
            Book store!
        </bold>
    </Item>
    <Item priority="2"> 
        book1
    </Item>
    <Item priority="2"> 
        book2
    </Item>
</Todo>
上面的是要读取的XML文件

下面的代码用来读取XML文件中的内容

#include "tinyxml.h"
#include <iostream>
#include <string>

using namespace std;

enum SuccessEnum {FAILURE,SUCCESS};

SuccessEnum loadXML()
{
    tixmlDocument doc;
    if(!doc.LoadFile("a.xml"))
    {
        cerr << doc.ErrorDesc() << endl;
        return FAILURE;
    }

    tixmlElement* root = doc.FirstChildElement();
    if(root == NULL)
    {
        cerr << "Failed to load file: No root element." << endl;
        doc.Clear();
        return FAILURE;
    }

    for(tixmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
    {
        string elemName = elem->Value();
        const char* attr;
        attr = elem->Attribute("priority");
        if(strcmp(attr,"1")==0)
        {
            tixmlElement* e1 = elem->FirstChildElement("bold");
            tixmlNode* e2=e1->FirstChild();
            cout<<"priority=1\t"<<e2->ToText()->Value()<<endl;

        }
        else if(strcmp(attr,"2")==0)
        {
            tixmlNode* e1 = elem->FirstChild();
            cout<<"priority=2\t"<<e1->ToText()->Value()<<endl;
        }
    }
    doc.Clear();
    return SUCCESS;
}

int main(int argc,char* argv[])
{
    if(loadXML() == FAILURE)
        return 1;
    return 0;
}


3.生成XML文件

生成文件b.xml如下所示:

<root>
    <Element1 attribute1="some value" />
    <Element2 attribute2="2" attribute3="3">
        <Element3 attribute4="4" />
        Some text.
    </Element2>
</root>

生成上面b.xmlL文件代码如下:

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

enum SuccessEnum {FAILURE,SUCCESS};

SuccessEnum saveXML()
{
    tixmlDocument doc;

    tixmlElement* root = new tixmlElement("root");
    doc.LinkEndChild(root);

    tixmlElement* element1 = new tixmlElement("Element1");
    root->LinkEndChild(element1);

    element1->SetAttribute("attribute1","some value");


    tixmlElement* element2 = new tixmlElement("Element2");  ///元素
    root->LinkEndChild(element2);

    element2->SetAttribute("attribute2","2");
    element2->SetAttribute("attribute3","3");


    tixmlElement* element3 = new tixmlElement("Element3");
    element2->LinkEndChild(element3);

    element3->SetAttribute("attribute4","4");

    tixmlText* text = new tixmlText("Some text.");  ///文本
    element2->LinkEndChild(text);


    bool success = doc.SaveFile("b.xml");
    doc.Clear();

    if(success)
        return SUCCESS;
    else
        return FAILURE;
}

int main(int argc,char* argv[])
{
    if(saveXML() == FAILURE)
        return 1;
    return 0;
}


4.重要函数或类型的说明

  (1)FirstChildElement(const char* value=0):获取一个值为value的子节点,value认值为空,则返回第一个子节点。

  (2)NextSiblingElement( const char* _value=0 ) :获得下一个(兄弟)节点。

  (3)LinkEndChild(XMLHandle *handle):添加一个子节点。元素或者文本

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念