C++代码库tinyxml2使用技巧

1 常规使用

const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
tinyxml2::XMLDocument doc; //创建一个tinyxml2库的XMLDocument对象,表示一个完整的XML文档
doc.Parse(declaration); //将XML声明解析为XMLDocument对象的一部分

const char* chroot = "OpenDRIVE";
tinyxml2::XMLElement* root = doc.NewElement(chroot); //创建一个tinyxml2库的XMLElement指针对象 
doc.InsertEndChild(root);

doc.SaveFile("output_opendrive.xodr"); //保存XML文档

示例1,对于XMLDocument对象,使用到的方法有ToDocument()NewElement()NewText();对于XMLElement对象,使用到的方法有SetAttribute()InsertFirstChild()InsertEndChild();对于XMLText对象,使用到的方法有SetCData()

tinyxml2::XMLDocument doc;
tinyxml2::XMLDocument* _p_doc = doc.ToDocument(); //ToDocument()函数返回一个指针,它指向XMLDocument对象 

tinyxml2::XMLElement* header = _p_doc->NewElement("header");
header->SetAttribute("reMajor", "1");
header->SetAttribute("revMinor", "4");
header->SetAttribute("name", "sample");
header->SetAttribute("version", "1.00");
...
tinyxml2::XMLElement* georef = _p_doc->NewElement("geoReference");
tinyxml2::XMLText* geotext = _p_doc->NewText("+proj=tmerc...+no_defs");
geotext->SetCData(true);
georef->InsertFirstChild(geotext); 
header->InsertEndChild(georef); 
root->InsertEndChild(header); 

生成如下xml内容,

<header revMajor="1" revMinor="4" name="sample" version="1.00" date="2023-07-17T21:53:12" north="-3.07576662199571729e+01" south="3.21219889912754297e+01" east="1.55376248724678589e+01" west="-1.44333776089006278e+01" vendor="Baidu">
    <geoReference><![CDATA[+proj=tmerc +ellps=WGS84 +lon_0=117 +lat_0=0 +x_0=-23022 +y_0=-4303190 +k=0.9996 +datum=WGS84 +units=m +no_defs]]></geoReference>
</header>

示例2,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值