【C++】【TinyXml】xml文件的讀寫功能使用——寫xml文件


TinyXml工具是常用比較簡單的C++中xml讀寫的工具

需要加載

#include "TinyXml\tinyxml.h"

在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的一些操作。

 

寫XML文件方法:

文檔類TiXmlDocument

TiXmlDocument doc;string outputFilePath = “E:\\text.xml”;
TiXmlElement *converterElement = new TiXmlElement("Converter");
doc.LinkEndChild(converterElement);
doc.SaveFile(outputFilePath.c_str());

元素結點類TiXmlElement

添加節點方法LinkEndChild(TiXmlNode* node)

設置節點屬性方法SetAttribute( const char * cname, const char * cvalue )

TiXmlDocument doc;string outputFilePath = “E:\\text.xml”;
TiXmlElement *converterElement = new TiXmlElement("Converter");
doc.LinkEndChild(converterElement);

TiXmlElement *configureElement = new TiXmlElement("Configure");
converterElement->LinkEndChild(configureElement);

TiXmlElement *generalElement = new TiXmlElement("Options");
configureElement->LinkEndChild(generalElement);
generalElement->SetAttribute("Name", "General");

doc.SaveFile(outputFilePath.c_str());

效果如下

<Converter>
    <Configure>
        <Options Name="General">
    </Configure>
</Converter>

內容類TiXmlText

TiXmlElement *OptionElement = new TiXmlElement("Option");
OptionElement->SetAttribute("Name", “Value”);
TiXmlText *NameContent = new TiXmlText(“text”);
OptionElement->LinkEndChild(NameContent);

return OptionElement;

效果如下

<Option Name="Value">text</Option>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM