在 Xerces-C 中从 DOMNode* 传递到 DOMElement*

发布于 2024-10-12 23:46:46 字数 1497 浏览 7 评论 0原文

我有一个操作 xml 的 C++ 应用程序。 好吧,在我的应用程序的某个时刻,我得到一个 DOMNode*,然后我将它作为子元素附加到一个元素。

好吧,问题是我想向该节点添加参数...好吧,它是一个节点,所以它不是一个元素...只有元素才有参数...

这是我的代码:

xercesc::DOMNode* node = 0;
std::string xml = from_an_obj_of_mine.GetXml(); /* A string with xml inside, the xml is sure an element having something inside */
xercesc::MemBufInputSource xml_buf((const XMLByte*)xml.c_str(), xml.size(), "dummy");
xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser();
parser->parse(xml_buf); /* parser will contain a DOMDocument well parsed from the string, I get here the node i want to attach */
node = my_pointer_to_a_preexisting_domdocument->GetXmlDocument()->importNode(parser->getDocument()->getDocumentElement(), true); /* I want to attach the node in parser to a node of my_pointer_to_an_el_of_my_preexisting_domdocument, it is a different tree, so I must import the node to attach it later */
my_pointer_to_an_el_of_my_preexisting_domdocument->appendChild(node);

如您所见,我想要从字符串创建一个节点,我通过解析创建它,然后需要导入该节点来创建属于我想要附加新节点的 dom 树的新的相同节点。 我的步骤是:

  • 获取要附加到预先存在的 dom 的 xml 字符串(在某处作为 domdocument 存储)

  • < p>创建一个解析器

  • 使用解析器从字符串创建一个dom树

  • 从我预先存在的dom(其中我想附加我的新节点),调用导入并克隆节点 这样它就可以附加到预先存在的 dom。

  • 附加它

问题是导入和导入给我一个节点...我想要附加一个元素...

我也使用appendChild来附加元素...当然该方法需要DOMNode * 但给它一个 DOMElement* (继承自 DOMNode)就可以了...

我怎样才能从节点获取元素??? 删除wd_parser;

I have a c++ application that manipulates xml.
Well, at a certain point of my application I get a DOMNode* and then I attach it to an element as a child.

Well the problem is that I would like to add parameters to that node... well it is a node so it is not an element... only elements have parameters...

This is my code:

xercesc::DOMNode* node = 0;
std::string xml = from_an_obj_of_mine.GetXml(); /* A string with xml inside, the xml is sure an element having something inside */
xercesc::MemBufInputSource xml_buf((const XMLByte*)xml.c_str(), xml.size(), "dummy");
xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser();
parser->parse(xml_buf); /* parser will contain a DOMDocument well parsed from the string, I get here the node i want to attach */
node = my_pointer_to_a_preexisting_domdocument->GetXmlDocument()->importNode(parser->getDocument()->getDocumentElement(), true); /* I want to attach the node in parser to a node of my_pointer_to_an_el_of_my_preexisting_domdocument, it is a different tree, so I must import the node to attach it later */
my_pointer_to_an_el_of_my_preexisting_domdocument->appendChild(node);

As you can see I want to create a node from a string, I create it through a parse and then need to import the node to create a new identical node belonging to the dom tree where I want to attach the new node.
My steps are:

  • Get the xml string to attach to a pre-existing dom (stored as a domdocument somewhere)

  • Create a parser

  • Using the parser create a dom tree from the string

  • From my pre-existing dom (where I want to attach my new node), call the import and clone the node
    so that it can be attached to the pre-existing dom.

  • Attach it

The problem is that import and import gets me a node... I want an element to attach...

I use appendChild to append elements too... of course the method wants DOMNode* but giving it a DOMElement* (which inherits from DOMNode) is ok...

How can I get an element from a node???
delete wd_parser;

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

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

发布评论

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

评论(1

南冥有猫 2024-10-19 23:46:46

好吧我发现了...

只需将节点重新转换为元素即可完成...DOMNode 是一个纯虚拟类,它是 DOMElement 的父类...所以它是正确的,也是做事的方式(从逻辑上讲)。

DOMElement* = dynamic_cast<DOMElement*>(node);

:)

ok I discovered it...

Just re-cast the node to element and it is done... DOMNode is a pure virtual class and it is parent of DOMElement... so it is correct and it is also the way to do things (logically speaking).

DOMElement* = dynamic_cast<DOMElement*>(node);

:)

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