如何解析RapidXML中的XML文件 [英] How to parse the XML file in RapidXML

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

问题描述

我必须在C ++中解析XML文件。我正在研究,发现RapidXml库为此。



我对 doc.parse< 0>(xml)怀疑。



可以<。code> xml 为.xml文件,或者需要一个字符串 char *



如果只能使用 string $ c> char * 然后我想我需要读取整个文件并将其存储在一个字符数组,并将它的指针传递给函数?



有一种方法可以直接使用文件,因为我需要更改代码中的XML文件。



如果在RapidXML中不可能,请在C ++中建议一些其他XML库。



谢谢!!!



Ashd

$ b $ RapidXml提供了一个类来为你执行此操作, rapidxml :: file 在<$ div c $ c> rapidxml_utils.hpp 文件。
类似:

  #includerapidxml_utils.hpp
$ b $ int int {
rapidxml :: file<> xmlFile(somefile.xml); //默认模板是char
rapidxml :: xml_document<> doc;
doc.parse< 0>(xmlFile.data());
...
}

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


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

I have doubt about doc.parse<0>(xml).

Can xml be .xml file or it needs to be a string or char *?

If can take only 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?

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

If it is not possible in RapidXML then please suggest some other XML libraries in C++.

Thanks!!!

Ashd

解决方案

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());
...
}

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天全站免登陆