gpt4 book ai didi

c++ - 使用 RapidXML 读取 XML 节点

转载 作者:行者123 更新时间:2023-11-28 07:01:31 26 4
gpt4 key购买 nike

我正在使用 RapidXML 来解析 XML 文件并读取节点内容,但我不想读取节点内的值,我需要以“XML 形式”而非解析值形式读取特定 XML 节点的内容。

示例:

<node1>
<a_lot_of_xml>
< .... >
</a_lot_of_xml>
</node1>

我需要获取 node1 的内容:

<a_lot_of_xml>
< .... >
</a_lot_of_xml>

我累了:

我尝试了一些东西,但我认为它不是很好,它即将放入 node1,这是另一个要读取的 xml 文件的路径,我这样做了:

<file1ToRead>MyFile.xml</file1ToRead>

然后我的 C++ 代码如下:

ifstream file(FileToRead);

stringstream buffer; buffer << file.rdbuf();

但问题是用户需要维护很多 XML 文件,而我只想使用一个 xml 文件。

最佳答案

我认为“大量的 XML 文件”是一种更好的方式,因此您拥有所有 xml 文件的目录,您可以在需要时读取 xml 文件,对性能有好处。回到问题,可以使用rapidxml::print函数获取xml格式。

bool test_analyze_xml(const std::string& xml_path)
{
try
{
rapidxml::file<> f_doc(xml_path.c_str());
rapidxml::xml_document<> xml_doc;
xml_doc.parse<0>(const_cast<char*>(f_doc.data()));
rapidxml::xml_node<>* node_1 = xml_doc.first_node("node1");
if(node_1 == NULL)
{
return false;
}
rapidxml::xml_node<>* plain_txt = node_1->first_node("a_lot_of_xml");
if (plain_txt == NULL)
{
return false;
}
std::string xml_data;
rapidxml::print(std::back_inserter(xml_data), *plain_txt, rapidxml::print_no_indenting); //the xml_data is XML format.
}
catch (...)
{
return false;
}
return true;
}

关于c++ - 使用 RapidXML 读取 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com