简体   繁体   English

C ++:如何从RapidXml中提取字符串

[英]C++: How to extract a string from RapidXml

In my C++ program I want to parse a small piece of XML, insert some nodes, then extract the new XML (preferably as a std::string ). 在我的C ++程序中,我想解析一小段XML,插入一些节点,然后提取新的XML(最好是作为std::string )。
RapidXml has been recommended to me, but I can't see how to retrieve the XML back as a text string. RapidXml已被推荐给我,但我看不到如何将XML作为文本字符串检索回来。
(I could iterate over the nodes and attributes and build it myself, but surely there's a build in function that I am missing.) (我可以迭代节点和属性并自己构建它,但肯定有一个我缺少的内置函数。)
Thank you. 谢谢。

Althoug the documentation is poor on this topic, I managed to get some working code by looking at the source. 虽然这个主题的文档很差,但我设法通过查看源代码获得了一些工作代码。 Although it is missing the xml header which normally contains important information. 虽然它缺少通常包含重要信息的xml标头。 Here is a small example program that does what you are looking for using rapidxml: 这是一个小例子程序,使用rapidxml执行您正在寻找的内容:

#include <iostream>
#include <sstream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_print.hpp"

int main(int argc, char* argv[]) {
    char xml[] = "<?xml version=\"1.0\" encoding=\"latin-1\"?>"
                 "<book>"
                 "</book>";

    //Parse the original document
    rapidxml::xml_document<> doc;
    doc.parse<0>(xml);
    std::cout << "Name of my first node is: " << doc.first_node()->name() << "\n";

    //Insert something
    rapidxml::xml_node<> *node = doc.allocate_node(rapidxml::node_element, "author", "John Doe");
    doc.first_node()->append_node(node);

    std::stringstream ss;
    ss <<*doc.first_node();
    std::string result_xml = ss.str();
    std::cout <<result_xml<<std::endl;
    return 0;
}

使用print函数(在rapidxml_print.hpp实用程序头中找到)将XML节点内容打印到stringstream

rapidxml::print reuqires an output iterator to generate the output, so a character string works with it. rapidxml :: print要求输出迭代器生成输出,因此字符串可以使用它。 But this is risky because I can not know whether an array with fixed length (like 2048 bytes) is long enough to hold all the content of the XML. 但这是有风险的,因为我无法知道具有固定长度(如2048字节)的数组是否足以容纳XML的所有内容。

The right way to do this is to pass in an output iterator of a string stream so allow the buffer to be expanded when the XML is being dumped into it. 执行此操作的正确方法是传入字符串流的输出迭代器,以便在将XML转储到其中时允许扩展缓冲区。

My code is like below: 我的代码如下:

std::stringstream stream;
std::ostream_iterator<char> iter(stream);

rapidxml::print(iter, doc, rapidxml::print_no_indenting);

printf("%s\n", stream.str().c_str());
printf("len = %d\n", stream.str().size());

Here's how to print a node to a string straight from the RapidXML Manual : 以下是直接从RapidXML手册将节点打印到字符串的方法

xml_document<> doc;    // character type defaults to char
// ... some code to fill the document

// Print to stream using operator <<
std::cout << doc;   

// Print to stream using print function, specifying printing flags
print(std::cout, doc, 0);   // 0 means default printing flags

// Print to string using output iterator
std::string s;
print(std::back_inserter(s), doc, 0);

// Print to memory buffer using output iterator
char buffer[4096];                      // You are responsible for making the buffer large enough!
char *end = print(buffer, doc, 0);      // end contains pointer to character after last printed character
*end = 0;                               // Add string terminator after XML

If you do build XML yourself, don't forget to escape the special characters. 如果您自己构建XML,请不要忘记转义特殊字符。 This tends to be overlooked, but can cause some serious headaches if it is not implemented: 这往往被忽视,但如果没有实施,可能会引起一些严重的问题:

  • < &lt; <&lt;
  • > &gt; >&gt;
  • & &amp; && amp;
  • " &quot; “”
  • ' &apos; ''

Use static_cast<> 使用static_cast <>

Ex: 例如:

rapidxml::xml_document<> doc;
rapidxml::xml_node <> * root_node = doc.first_node();
std::string strBuff;

doc.parse<0>(xml);

.
.
.
strBuff = static_cast<std::string>(root_node->first_attribute("attribute_name")->value());

Following is very easy, 以下很容易,

std::string s;
print(back_inserter(s), doc, 0);
cout << s;

You only need to include "rapidxml_print.hpp" header in your source code. 您只需在源代码中包含“rapidxml_print.hpp”标头。

If you aren't yet committed to Rapid XML, I can recommend some alternative libraries: 如果您尚未致力于Rapid XML,我可以推荐一些替代库:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM