如何使用 RapidXml for C++ 在 xml_document 中插入新节点使用字符串?

发布于 2024-10-11 21:34:38 字数 1074 浏览 6 评论 0原文

std::string src = "<xml><node1>aaa</node1><node2>bbb</node2><node1>ccc</node1></xml>";
std::string src2 = "<nodex>xxx</nodex>";

我想使用 RapidXml 将 src2 中的节点附加到 src 中的树中 我这样做:

xml_document<> xmldoc;
xml_document<> xmlseg;
std::vector<char> s(src.begin(), src.end());
std::vector<char> x(src2.begin(), src2.end());
xmldoc.parse<0>(&s[0]);
xmlseg.parse<0>(&x[0]);
xml_node<>* a = xmlseg.first_node(); /* Node to append */
xmldoc.first_node("xml")->append_node(a); /* Appending node a to the tree in src */

嗯,编译得很好,但是运行时我遇到了这个可怕的错误:

无效 rapidxml::xml_node::append_node(rapidxml::xml_node*) [with Ch = char]: 断言 `child && !child->parent() && child->type() != node_document' 失败。已中止

不知道怎么办。 问题很简单,我需要将一个节点附加到树(xml),但我有字符串。

我想发生这种情况是因为我试图将一棵树的节点插入另一棵树...只有为给定树分配的节点才能添加到该树...这很糟糕...

有什么办法我可以做我需要什么简单的方式?

谢谢。

std::string src = "<xml><node1>aaa</node1><node2>bbb</node2><node1>ccc</node1></xml>";
std::string src2 = "<nodex>xxx</nodex>";

I want to append the node in src2 inside the tree in src using RapidXml
I do this:

xml_document<> xmldoc;
xml_document<> xmlseg;
std::vector<char> s(src.begin(), src.end());
std::vector<char> x(src2.begin(), src2.end());
xmldoc.parse<0>(&s[0]);
xmlseg.parse<0>(&x[0]);
xml_node<>* a = xmlseg.first_node(); /* Node to append */
xmldoc.first_node("xml")->append_node(a); /* Appending node a to the tree in src */

Well, great it compiles, but when running I got this horrible error:

void
rapidxml::xml_node::append_node(rapidxml::xml_node*)
[with Ch = char]: Assertion `child &&
!child->parent() && child->type() !=
node_document' failed. Aborted

I don't know how to do.
The problem is simple I need to append a node to a tree (xml) but I have strings.

I guess this happens because I'm trying to insert a node of a tree into another tree... only nodes allocated for a given tree can be added to that tree... this sucks...

Is there a way I can do what I need in a simple way?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-10-18 21:34:38
#include <iostream>
#include <string>
#include <vector>

#include <rapidxml.hpp>
#include <rapidxml_print.hpp>

int main(){
std::string src = "<xml><node1>aaa</node1><node2>bbb</node2><node1>ccc</node1></xml>";
std::string src2 = "<nodex><nodey>xxx</nodey></nodex>";
//std::string src2 = "<nodex>xxx</nodex>";
rapidxml::xml_document<> xmldoc;
rapidxml::xml_document<> xmlseg;

std::vector<char> s( src.begin(), src.end() );
s.push_back( 0 ); // make it zero-terminated as per RapidXml's docs

std::vector<char> x(src2.begin(), src2.end());
x.push_back( 0 ); // make it zero-terminated as per RapidXml's docs

xmldoc.parse<0>( &s[ 0 ] );
xmlseg.parse<0>( &x[0] );

std::cout << "Before:" << std::endl;
rapidxml::print(std::cout, xmldoc, 0);

rapidxml::xml_node<>* a = xmlseg.first_node(); /* Node to append */

rapidxml::xml_node<> *node = xmldoc.clone_node( a );
//rapidxml::xml_node<> *node = xmldoc.allocate_node( rapidxml::node_element, a->name(), a->value() );
xmldoc.first_node("xml")->append_node( node ); /* Appending node a to the tree in src */

std::cout << "After :" << std::endl;
rapidxml::print(std::cout, xmldoc, 0); 
}

输出:

<xml>
        <node1>aaa</node1>
        <node2>bbb</node2>
        <node1>ccc</node1>
</xml>

After :
<xml>
        <node1>aaa</node1>
        <node2>bbb</node2>
        <node1>ccc</node1>
        <nodex>
                <nodey>xxx</nodey>
        </nodex>
</xml>
#include <iostream>
#include <string>
#include <vector>

#include <rapidxml.hpp>
#include <rapidxml_print.hpp>

int main(){
std::string src = "<xml><node1>aaa</node1><node2>bbb</node2><node1>ccc</node1></xml>";
std::string src2 = "<nodex><nodey>xxx</nodey></nodex>";
//std::string src2 = "<nodex>xxx</nodex>";
rapidxml::xml_document<> xmldoc;
rapidxml::xml_document<> xmlseg;

std::vector<char> s( src.begin(), src.end() );
s.push_back( 0 ); // make it zero-terminated as per RapidXml's docs

std::vector<char> x(src2.begin(), src2.end());
x.push_back( 0 ); // make it zero-terminated as per RapidXml's docs

xmldoc.parse<0>( &s[ 0 ] );
xmlseg.parse<0>( &x[0] );

std::cout << "Before:" << std::endl;
rapidxml::print(std::cout, xmldoc, 0);

rapidxml::xml_node<>* a = xmlseg.first_node(); /* Node to append */

rapidxml::xml_node<> *node = xmldoc.clone_node( a );
//rapidxml::xml_node<> *node = xmldoc.allocate_node( rapidxml::node_element, a->name(), a->value() );
xmldoc.first_node("xml")->append_node( node ); /* Appending node a to the tree in src */

std::cout << "After :" << std::endl;
rapidxml::print(std::cout, xmldoc, 0); 
}

Output:

<xml>
        <node1>aaa</node1>
        <node2>bbb</node2>
        <node1>ccc</node1>
</xml>

After :
<xml>
        <node1>aaa</node1>
        <node2>bbb</node2>
        <node1>ccc</node1>
        <nodex>
                <nodey>xxx</nodey>
        </nodex>
</xml>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文