Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用RapidXml解析XML文件 我必须解析C++中的XML文件。我正在研究并找到了用于此的RapidXml库_C++_Xml_Parsing_Rapidxml - Fatal编程技术网

如何使用RapidXml解析XML文件 我必须解析C++中的XML文件。我正在研究并找到了用于此的RapidXml库

如何使用RapidXml解析XML文件 我必须解析C++中的XML文件。我正在研究并找到了用于此的RapidXml库,c++,xml,parsing,rapidxml,C++,Xml,Parsing,Rapidxml,我对doc.parse(xml)有疑问 xml可以是.xml文件,还是需要是字符串或char* 如果我只能使用string或char*那么我想我需要读取整个文件并将其存储在char数组中,然后将其指针传递给函数 是否有直接使用文件的方法,因为我还需要在代码中更改XML文件 如果在RapidXml不可能,那么请在C++中建议一些其他XML库。 谢谢 Ashd告诉我们: 函数xml_document::parse […]解析以零结尾的XML字符串 根据给定的旗帜 RapidXML将从文件加载字符数据

我对
doc.parse(xml)
有疑问

xml
可以是.xml文件,还是需要是
字符串
char*

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

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

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

Ashd告诉我们:

函数xml_document::parse

[…]解析以零结尾的XML字符串 根据给定的旗帜


RapidXML将从文件加载字符数据留给您。或者像建议的那样将文件读入缓冲区,或者使用一些内存映射技术。(但查找<代码> PARSENIONNEORION < /COD>标志>)< /P> < P>新到C++我自己…但我想分享一个解决方案

YMMV

向SiCrane大声说: --然后用向量替换“字符串”——(谢谢anno)

请评论并帮助我学习!我对这个很陌生

无论如何,这似乎是一个好的开始:

#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('\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 */


}
#包括
#包括
#包括
#包括“../../rapidxml/rapidxml.hpp”
使用名称空间std;
int main(){
ifstream myfile(“sampleconfig.xml”);
rapidxml::xml_文档文档;
/*“将文件读入向量”请参见上面的链接线程*/
向量缓冲区((istreambuf_迭代器(myfile)),istreambuf_迭代器();
buffer.push_back('\0');

我们通常将磁盘上的XML读入
std::string
,然后将其安全复制到
std::vector
,如下所示:

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('\0');

// 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]);
字符串输入\u xml;
弦线;
ifstreamin(“demo.xml”);
//将文件读入输入xml
while(getline(in,line))
输入_xml+=行;
//安全地修改输入xml的副本
//(永远不要直接修改std::string的内容)
向量xml_copy(input_xml.begin(),input_xml.end());
xml_copy.push_back('\0');
//从现在开始只使用xml_copy!
xml_文档文档;
//我们选择解析XML声明
//parse_no_data_节点可防止RapidXML使用令人惊讶的
//同时具有值和数据节点的行为,并使数据节点
//打印时优先于值

//>>>请注意,这将跳过CDATA节点的解析RapidXml附带了一个类来为您执行此操作,
RapidXml::file
位于
RapidXml\u 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());
...
}
#包括“rapidxml_utils.hpp”
int main(){
rapidxml::file xmlFile(“somefile.xml”);//默认模板为char
rapidxml::xml_文档文档;
parse(xmlFile.data());
...
}

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

xml_document::parse()的参数是包含xml的以零结尾的字符串。因此,您只需创建一个file2string函数。将文件读入向量缓冲区,然后将&buffer[0]传递给parse()这很有效,但是只有当“向量缓冲区”没有落入范围之外:解决这个问题的一个快速而肮脏的方法是把“静态”关键字添加到向量中,但是我认为这不是很干净。请看:由于向量大小的调整,这太慢了。相比于乔恩的超答案,它快得多。我是新的C++,但是函数x在哪里呢?MLFILE()定义在?我不能在任何标题文件中找到它。XMLFILE是一个变量而不是函数-所以这是定义,为什么它要取一个参数?C++中意味着什么?