XmlNode’s copy constructor now recursively copies the children nodes as well
This commit is contained in:
+9
-2
@@ -151,9 +151,8 @@ int main()
|
|||||||
std::cout << color << "Testing XML parsing…" << reset << std::endl;
|
std::cout << color << "Testing XML parsing…" << reset << std::endl;
|
||||||
XmppParser xml;
|
XmppParser xml;
|
||||||
const std::string doc = "<stream xmlns='stream_ns'><stanza b='c'>inner<child1><grandchild/></child1><child2 xmlns='child2_ns'/>tail</stanza></stream>";
|
const std::string doc = "<stream xmlns='stream_ns'><stanza b='c'>inner<child1><grandchild/></child1><child2 xmlns='child2_ns'/>tail</stanza></stream>";
|
||||||
xml.add_stanza_callback([](const Stanza& stanza)
|
auto check_stanza = [](const Stanza& stanza)
|
||||||
{
|
{
|
||||||
std::cout << stanza.to_string() << std::endl;
|
|
||||||
assert(stanza.get_name() == "stanza");
|
assert(stanza.get_name() == "stanza");
|
||||||
assert(stanza.get_tag("xmlns") == "stream_ns");
|
assert(stanza.get_tag("xmlns") == "stream_ns");
|
||||||
assert(stanza.get_tag("b") == "c");
|
assert(stanza.get_tag("b") == "c");
|
||||||
@@ -163,6 +162,14 @@ int main()
|
|||||||
assert(stanza.get_child("child2", "stream_ns") == nullptr);
|
assert(stanza.get_child("child2", "stream_ns") == nullptr);
|
||||||
assert(stanza.get_child("child2", "child2_ns") != nullptr);
|
assert(stanza.get_child("child2", "child2_ns") != nullptr);
|
||||||
assert(stanza.get_child("child2", "child2_ns")->get_tail() == "tail");
|
assert(stanza.get_child("child2", "child2_ns")->get_tail() == "tail");
|
||||||
|
};
|
||||||
|
xml.add_stanza_callback([check_stanza](const Stanza& stanza)
|
||||||
|
{
|
||||||
|
std::cout << stanza.to_string() << std::endl;
|
||||||
|
check_stanza(stanza);
|
||||||
|
// Do the same checks on a copy of that stanza.
|
||||||
|
Stanza copy(stanza);
|
||||||
|
check_stanza(copy);
|
||||||
});
|
});
|
||||||
xml.feed(doc.data(), doc.size(), true);
|
xml.feed(doc.data(), doc.size(), true);
|
||||||
|
|
||||||
|
|||||||
@@ -35,9 +35,8 @@ public:
|
|||||||
node.parent = nullptr;
|
node.parent = nullptr;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* The copy constructor do not copy the children or parent attributes. The
|
* The copy constructor do not copy the parent attribute. The children
|
||||||
* copied node is identical to the original except that it is not attached
|
* nodes are all copied recursively.
|
||||||
* to any other node.
|
|
||||||
*/
|
*/
|
||||||
XmlNode(const XmlNode& node):
|
XmlNode(const XmlNode& node):
|
||||||
name(node.name),
|
name(node.name),
|
||||||
@@ -48,6 +47,11 @@ public:
|
|||||||
inner(node.inner),
|
inner(node.inner),
|
||||||
tail(node.tail)
|
tail(node.tail)
|
||||||
{
|
{
|
||||||
|
for (XmlNode* child: node.children)
|
||||||
|
{
|
||||||
|
XmlNode* child_copy = new XmlNode(*child);
|
||||||
|
this->add_child(child_copy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~XmlNode();
|
~XmlNode();
|
||||||
|
|||||||
Reference in New Issue
Block a user