TinyXML 操作XML

0. 原文链接:

https://www.cnblogs.com/hgwang/p/5833638.html

1. 介绍

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

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

官方介绍文档:http://www.grinninglizard.com/tinyxmldocs/tutorial0.html

在TinyXML中,根据XML的各种元素来定义了一些类:

TiXmlBase:整个TinyXML模型的基类。

TiXmlAttribute:对应于XML中的元素的属性。

TiXmlNode:对应于DOM结构中的节点。

TiXmlComment:对应于XML中的注释

TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。

TiXmlDocument:对应于XML的整个文档。

TiXmlElement:对应于XML的元素。

TiXmlText:对应于XML的文字部分

TiXmlUnknown:对应于XML的未知部分。

TiXmlHandler:定义了针对XML的一些操作。

根据下图1来说明常用的类对应的文本格式:

<?xml version="1.0" ?> 				    //TiXmlDeclaration,声明
<MyApp>    						        //TiXmlElement,元素
    <!-- Settings for MyApp -->		    //TiXmlComment,注释
    <Messages>					        //TiXmlElement,元素
        <Welcome>Welcome to MyApp</Welcome>
//<Welcome>是元素TiXmlElement ,“Welcome to MyApp”是TiXmlText,文本
        <Farewell>Thank you for using MyApp</Farewell>	//同上
    </Messages>
    <Windows>					        //TiXmlElement,元素
        <Window name="MainFrame" x="5" y="15" w="400" h="250" />
// Window是元素TiXmlElement ,name、x、y、h是TiXmlAttribute
    </Windows>
    <Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp>

 

TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。

注意,TiXmlBase 是TiXmlNode的基类,TiXmlNode是TiXmlElement、TiXmlComment、TiXmlText、TiXmlDeclaration、TiXmlUnknown、TiXmlAttribute的基类。

TiXmlBase
    |__TiXmlNode
                |__TiXmlAttribute
                |__TiXmlComment
                |__TiXmlDeclaration
                |__TiXmlElement
                |__TiXmlText
                |__TiXmlUnknown

2. TinyXML配置 

在stdafx.h头文件中增加头文件引用#include "tinyxml/tinyxml.h"

在工程设置中加入lib引用库

在stdafx.h中加入动态库引用

#ifdef _DEBUG
#pragma comment(lib,"TinyXMLD.lib")
#else#pragma comment(lib,"TinyXML.lib")
#endif

个人理解:这里之所以这么写是因为作者将6个TinyXML文件作成了DLL,也可以直接将6个文件拷贝到工程目录下使用。

3. TinyXML读取和保存文件 

3.1 读取xml文件

TiXmlDocument lconfigXML;if( !lconfigXML.LoadFile( strXmlFile.c_str() ) )
{
    break;
}

3.2 读取xml参数(parse函数将xml格式的字符串转换为XML文档模型

TiXmlDocument lActionXML;
lActionXML.Parse(strRmcpParam.c_str());if(lActionXML.Error())
{
     strErr = "输入参数不是标准的xml格式";
     return false;
 }

3.3 保存xml参数到文本

TiXmlDocument tyDoc;
…
tyDoc.SaveFile(m_strFilePath);

3.4 保存xml参数到临时变量

TiXmlDocument tyDoc;
…
TiXmlPrinter printer;
tyDoc.Accept(&printer);
std::string devParam = std::string(printer.CStr());

4. TinyXML增删改查

4.1 增

创建一个如1.介绍中图1所示的xml文件,代码如下:

void write_app_settings_doc( )  
{  
    TiXmlDocument doc;  
    TiXmlElement* msg;
     TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
    doc.LinkEndChild( decl );   
    TiXmlElement * root = new TiXmlElement( "MyApp" );  
    doc.LinkEndChild( root );  
    TiXmlComment * comment = new TiXmlComment();
    comment->SetValue(" Settings for MyApp " );  
    root->LinkEndChild( comment );   
    TiXmlElement * msgs = new TiXmlElement( "Messages" );  
    root->LinkEndChild( msgs );   
    msg = new TiXmlElement( "Welcome" );  
    msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));  
    msgs->LinkEndChild( msg );   
    msg = new TiXmlElement( "Farewell" );  
    msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));  
    msgs->LinkEndChild( msg );   
    TiXmlElement * windows = new TiXmlElement( "Windows" );  
    root->LinkEndChild( windows );  
    TiXmlElement * window;
    window = new TiXmlElement( "Window" );  
    windows->LinkEndChild( window );  
    window->SetAttribute("name", "MainFrame");
    window->SetAttribute("x", 5);
    window->SetAttribute("y", 15);
    window->SetAttribute("w", 400);
    window->SetAttribute("h", 250);
    TiXmlElement * cxn = new TiXmlElement( "Connection" );  
    root->LinkEndChild( cxn );  
    cxn->SetAttribute("ip", "192.168.0.1");
    cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib
    dump_to_stdout( &doc );
    doc.SaveFile( "appsettings.xml" );  
}

在节点最后插入新节点:

TiXmlNode* LinkEndChild( TiXmlNode* addThis );

在节点前/后插入新节点:

TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );

4.2 删

删除某个节点, TiXmlNode是TiXmlElement、TiXmlComment、TiXmlText、TiXmlDeclaration、TiXmlUnknown、TiXmlAttribute的基类。

TiXmlNode node;
node.Clear();

从A节点上移除子节点B:

TiXmlNode nodeA;
nodeA. RemoveChild( TiXmlNode* removeThis );

从元素A上移除名字为B的属性:

TiXmlAttribute attrA;
attrA. RemoveAttribute( const char * name );

4.3 改

查找内容为<mfid val="1234" />,现需要将1234改成其他值:

TiXmlNode* lpnode = NULL;
lpnode = tixml.RootElement()->IterateChildren("mfid",lpnode);
TiXmlAttribute* tiattr = lpnode->ToElement()->FirstAttribute();
//找到mfid节点,获取第一个属性值。注意,如果有多个属性值,需要判断哪个属性值是需要的
tiattr->SetValue(mfid.c_str());

替换一个节点:

TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis ); 

4.4 查

获取link节点:

std::string strType = lpItemNode->ToElement()->Attribute("type");

遍历节点:

const TiXmlNode* lpMapNode = NULL; //初始化
lpMapNode = lconfigXML.RootElement()->IterateChildren("node", lpMapNode);
if (lpMapNode) 
{
    rms::CStationMapping litem;
    const TiXmlNode* lpItemNode = NULL ;
    while(lpItemNode = lpMapNode->IterateChildren("item",lpItemNode))
    {
        string str = lpItemNode->ToElement()->Attribute("ABC"); 
    }
}

遍历元素属性:

TiXmlAttribute* pAttr = NULL; for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())  
{    
  …
}

节点的下一个兄弟节点:

const TiXmlNode* NextSibling() const;

元素的下一个元素:

const TiXmlElement* NextSiblingElement() const;

属性的下一个属性:

const TiXmlAttribute* Next() const;

返回值为NULL表示不存在。

5. 重要函数或类型说明 

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值