C++ 如何使用 RapidXml 解析 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2808022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse an XML file with RapidXml
提问by ashd
I have to parse an XML file in C++. I was researching and found the RapidXml library for this.
我必须用 C++ 解析一个 XML 文件。我正在研究并为此找到了 RapidXml 库。
I have doubts about doc.parse<0>(xml)
.
我有疑问doc.parse<0>(xml)
。
Can xml
be an .xml file or does it need to be a string
or char *
?
可以xml
是 .xml 文件还是需要是string
或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?
如果我只能使用string
或者char *
我想我需要读取整个文件并将其存储在一个字符数组中并将它的指针传递给函数?
Is there a way to directly use a file because I would need to change the XML file inside the code also.
有没有办法直接使用文件,因为我还需要更改代码中的 XML 文件。
If that is not possible in RapidXml then please suggest some other XML libraries in C++.
如果这在 RapidXml 中是不可能的,那么请推荐一些 C++ 中的其他 XML 库。
Thanks!!!
谢谢!!!
Ashd
阿什德
回答by Superfly Jon
RapidXml comes with a class to do this for you, rapidxml::file
in the rapidxml_utils.hpp
file.
Something like:
RapidXmlrapidxml::file
在rapidxml_utils.hpp
文件中提供了一个类来为您执行此操作。就像是:
#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.
请注意,该xmlFile
对象现在包含 XML 的所有数据,这意味着一旦它超出范围并被销毁,doc 变量将不再安全可用。如果您在函数内部调用 parse,则必须以某种方式将xmlFile
对象保留在内存中(全局变量、new 等),以便文档保持有效。
回答by FlipMcF
New to C++ myself... but I wanted to share a solution.
我自己是 C++ 新手……但我想分享一个解决方案。
YMMV!
天啊!
Shout Out to SiCrane on this thread: -- and just replacing 'string' with a vector --- (thanks anno)
在此线程上向 SiCrane 大喊大叫:-- 只需将“字符串”替换为向量 ---(感谢 anno)
Please comment and help me learn also! I'm very new to this
请评论并帮助我学习!我对此很陌生
Anyway, this seems to work for a good start:
无论如何,这似乎是一个好的开始:
#include <iostream>
#include <fstream>
#include <vector>
#include "../../rapidxml/rapidxml.hpp"
using namespace std;
int main(){
ifstream myfile("sampleconfig.xml");
rapidxml::xml_document<> doc;
/* "Read file into vector<char>" See linked thread above*/
vector<char> buffer((istreambuf_iterator<char>(myfile)), istreambuf_iterator<char>( ));
buffer.push_back('string input_xml;
string line;
ifstream in("demo.xml");
// read file into input_xml
while(getline(in,line))
input_xml += line;
// make a safe-to-modify copy of input_xml
// (you should never modify the contents of an std::string directly)
vector<char> xml_copy(input_xml.begin(), input_xml.end());
xml_copy.push_back('##代码##');
// only use xml_copy from here on!
xml_document<> doc;
// we are choosing to parse the XML declaration
// parse_no_data_nodes prevents RapidXML from using the somewhat surprising
// behavior of having both values and data nodes, and having data nodes take
// precedence over values when printing
// >>> note that this will skip parsing of CDATA nodes <<<
doc.parse<parse_declaration_node | parse_no_data_nodes>(&xml_copy[0]);
');
cout<<&buffer[0]<<endl; /*test the buffer */
doc.parse<0>(&buffer[0]);
cout << "Name of my first node is: " << doc.first_node()->name() << "\n"; /*test the xml_document */
}
回答by karlphillip
We usually read the XML from the disk into a std::string
, then make a safe copy of it into a std::vector<char>
as demonstrated below:
我们通常将 XML 从磁盘读取到 a 中std::string
,然后将其安全复制到 a 中std::vector<char>
,如下所示:
For a complete source code check:
对于完整的源代码检查:
回答by Martin Ba
The manualtells us:
该手册告诉我们:
function xml_document::parse
[...] Parses zero-terminated XML string according to given flags.
函数 xml_document::parse
[...] 根据给定的标志解析以零结尾的 XML 字符串。
RapidXML leaves loading the character data from a file to you. Either read the file into a buffer, like annosuggested or alternatively use some memory mapping technique. (But look up parse_non_destructive
flag first.)
RapidXML 让您从文件中加载字符数据。要么将文件读入缓冲区,就像anno建议的那样,要么使用一些内存映射技术。(但parse_non_destructive
首先查找标志。)