tinyxml 读取文本节点_TinyXML读取xml文件

TinyXML读取xml文件

2020-05-19

编程之家收集整理的这篇文章主要介绍了TinyXML读取xml文件,编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例子 xml 内容: <?xml version="1.0" encoding="UTF-8" ?> 电视剧电影

以下为示例代码,buffer 中保存着上面例子的 xml 内容:

voidCxmlDlg::ParseXML()

{

char*buffer="<?xmlversion =\"1.0\"encoding=\"UTF-8\"?>\

\

\

\

电视剧\

电影\

\

";

charutf8[256]={0};

if(MBSToUTF8(utf8,sizeof(utf8),buffer)<=0)//此函数见我之前关于TinyXML的文章

return;

TiXmlDocumentdoc;

doc.Parse(utf8);

TiXmlElement*databaseElement=0;

TiXmlElement*listElement=0;

TiXmlElement*channelElement=0;

TiXmlHandledocHandle(&doc);

databaseElement=docHandle.FirstChild("Config").FirstChild(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TinyXML2是一个轻量级的XML解析库,可以方便地读取和修改XML文件。下面是一个简单的示例,演示如何使用TinyXML2读取和修改XML文件。 首先,需要包含TinyXML2的头文件: ```c++ #include "tinyxml2.h" using namespace tinyxml2; ``` 然后,可以使用下面的代码打开XML文件: ```c++ XMLDocument doc; doc.LoadFile("example.xml"); ``` 这将加载名为"example.xml"的XML文件。如果文件不存在或格式不正确,将返回错误代码。 读取节点的值可以使用以下代码: ```c++ XMLElement* root = doc.FirstChildElement("root"); const char* value = root->FirstChildElement("child")->GetText(); ``` 这将获取根节点中名为"child"的子节点文本值。 要修改节点的值,可以使用以下代码: ```c++ XMLElement* root = doc.FirstChildElement("root"); XMLElement* child = root->FirstChildElement("child"); child->SetText("new value"); ``` 这将把名为"child"的子节点文本值修改为"new value"。 最后,可以使用以下代码将修改后的XML文件保存到磁盘: ```c++ doc.SaveFile("example.xml"); ``` 完整的示例代码如下所示: ```c++ #include "tinyxml2.h" using namespace tinyxml2; int main() { XMLDocument doc; doc.LoadFile("example.xml"); XMLElement* root = doc.FirstChildElement("root"); const char* value = root->FirstChildElement("child")->GetText(); printf("Old value: %s\n", value); XMLElement* child = root->FirstChildElement("child"); child->SetText("new value"); doc.SaveFile("example.xml"); return 0; } ``` 这将加载名为"example.xml"的文件,读取名为"child"的子节点的值,并将其修改为"new value",最后将修改后的文件保存到磁盘。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值