如何使用RapidXml解析XML文件 [英] How to parse an XML file with RapidXml

查看:140
本文介绍了如何使用RapidXml解析XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析C ++中的XML文件.我正在研究中,并为此找到了RapidXml库.

I have to parse an XML file in C++. I was researching and found the RapidXml library for this.

我对doc.parse<0>(xml)有疑问.

xml可以是.xml文件,还是需要是stringchar *?

Can xml be an .xml file or does it need to be a string or char *?

如果我只能使用stringchar *,那么我想我需要读取整个文件并将其存储在char数组中,并将其指针传递给函数?

If I can only use string or char * then I guess I need to read the whole file and store it in a char array and pass the pointer of it to the function?

是否有一种直接使用文件的方法,因为我还需要在代码内更改XML文件.

Is there a way to directly use a file because I would need to change the XML file inside the code also.

如果RapidXml无法做到这一点,那么建议使用C ++推荐其他XML库.

If that is not possible in RapidXml then please suggest some other XML libraries in C++.

谢谢!

Ashd

推荐答案

RapidXml带有一个为您执行此操作的类,位于rapidxml_utils.hpp文件中的rapidxml::file. 像这样:

RapidXml comes with a class to do this for you, rapidxml::file in the rapidxml_utils.hpp file. Something like:

#include "rapidxml_utils.hpp"

int main() {
    rapidxml::file<> xmlFile("somefile.xml"); // Default template is char
    rapidxml::xml_document<> doc;
    doc.parse<0>(xmlFile.data());
...
}

请注意,xmlFile对象现在包含XML的所有数据,这意味着一旦它超出范围并被销毁,doc变量将不再安全可用.如果在函数内部调用parse,则必须以某种方式将xmlFile对象保留在内存中(全局变量,new等),以使文档保持有效.

Note that the xmlFile object now contains all of the data for the XML, which means that once it goes out of scope and is destroyed the doc variable is no longer safely usable. If you call parse inside of a function, you must somehow retain the xmlFile object in memory (global variable, new, etc) so that the doc remains valid.

这篇关于如何使用RapidXml解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆