将 Xerces-C DOMElement 从 Xerces-C DOMDocument 附加到另一个 Xerces-C DOMElement
我之前在RapidXml中询问过类似的问题,我想知道,现在,同样的问题,但使用Xerces-C。
我正在开发一个需要解析 xml 的 C++ 应用程序。
考虑以下内容:
xml 文件:file1.xml
<root>
<node1>value1</node1>
<node2>value2</node2>
</root>
xml 文件:file2.xml
<anotherroot>
<anothernode1>anothervalue1</anothernode1>
<anothernode2>anothervalue2</anothernode2>
</anotherroot>
我的 cpp 文件
using namespace xercesc;
// First tree
XercesDOMParser* parser1 = new XercesDOMParser();
parser1->parse("file1.xml"); // Loading xml and building tree (XercesDOMParser owns the document)
DOMDocument* doc1 = parser1->getDocument();
DOMElement* el1 = doc1->getDocumentElement(); // Getting root
// Second tree
XercesDOMParser* parser2 = new XercesDOMParser();
parser2->parse("file2.xml"); // Loading xml and building tree (XercesDOMParser owns the document)
DOMDocument* doc2 = parser2->getDocument();
DOMElement* el2 = doc2->getDocumentElement(); // Getting root
我想这样做:
el2->appendChild(el1);
以便文档 doc2 中的最终 xml 是
<anotherroot>
<anothernode1>anothervalue1</anothernode1>
<anothernode2>anothervalue2</anothernode2>
<root>
<node1>value1</node1>
<node2>value2</node2>
</root>
</anotherroot>
但是这样做时我得到:
抛出后终止调用 的实例 “xercesc_3_1::DOMException”已中止
我猜是因为我想要附加的元素属于另一棵树。我怎样才能达到这个结果?问题基本上是我有一棵树和一个包含 xml 段的字符串。 我需要解析字符串获取表示该 xml 的 DOM 对象并将其附加到另一棵树的节点。 最重要的是我有里面有 xml 的字符串...我无法绕过这个重要的要求。从字符串中获取 dom 并附加它。 似乎是不可能的事情……可能吗?
我该怎么办? 我真的无法接受这样一个事实:Xerces-C 程序员从未想到过这样的场景,也没有提供合理的功能来实现这样的解决方案。
即使我能告诉我是否有办法更改节点或元素的节点所有权,也许就足够了。你看,当我执行之前尝试的操作时,会出现 WRONG_DOCUMENT_ERR 错误。好吧,如果库提供了一种方法来更改节点的所有权,使其属于另一个文档,那么我就可以了,我的问题就可以解决!
谢谢
I questioned before about a similar problem in RapidXml, I want to know, now, the same but using Xerces-C.
I am working on a c++ application that needs to parse xml.
Consider the following:
xml file: file1.xml
<root>
<node1>value1</node1>
<node2>value2</node2>
</root>
xml file: file2.xml
<anotherroot>
<anothernode1>anothervalue1</anothernode1>
<anothernode2>anothervalue2</anothernode2>
</anotherroot>
my cpp file
using namespace xercesc;
// First tree
XercesDOMParser* parser1 = new XercesDOMParser();
parser1->parse("file1.xml"); // Loading xml and building tree (XercesDOMParser owns the document)
DOMDocument* doc1 = parser1->getDocument();
DOMElement* el1 = doc1->getDocumentElement(); // Getting root
// Second tree
XercesDOMParser* parser2 = new XercesDOMParser();
parser2->parse("file2.xml"); // Loading xml and building tree (XercesDOMParser owns the document)
DOMDocument* doc2 = parser2->getDocument();
DOMElement* el2 = doc2->getDocumentElement(); // Getting root
I would like to do this:
el2->appendChild(el1);
So that the final xml in the document doc2 is
<anotherroot>
<anothernode1>anothervalue1</anothernode1>
<anothernode2>anothervalue2</anothernode2>
<root>
<node1>value1</node1>
<node2>value2</node2>
</root>
</anotherroot>
But when doing so I get:
terminate called after throwing an
instance of
'xercesc_3_1::DOMException' Aborted
I guess because the element I want to attach belongs to another tree. How can I achieve this result? The problem, basically, is that I have a tree and a string containing an xml segment.
I NEED TO PARSE THE STRING get a DOM object representing that xml and attaching to a node of the other tree.
The most important thing is that I have the string with the xml inside... I cannot bypass this important requirement. From a string, getting the dom and attaching it.
It seems to be something impossible... possible?
How can I do this???
I really cannot accept the fact that Xerces-C programmers never figured such a scenario and did not provide a reasonable functionality to achieve such solution.
Maybe it would be enough even if I coult tell me whether there is a way of CHANGING THE NODE OWNERSHIP os a node or an element. You see, there is the WRONG_DOCUMENT_ERR which is raised when what I tried before is performed. Well, If the library provided a way to change the ownership of a node so that it belongs to another document, I would be all right and my problem would be solved!
Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DOMDocument::importNode 是一个 DOM Level 2 函数 旨在解决这个问题:
DOMDocument::importNode is a DOM Level 2 function that was designed to solve this exact problem: