在Xerces-C中从DOMNode *传递到DOMElement * [英] Passing from a DOMNode* to a DOMElement* in Xerces-C
问题描述
嗯,在我的应用程序的某一点我得到一个DOMNode *,然后我把它附加到一个元素作为一个孩子。
问题是我想添加参数到该节点...它是一个节点,所以它不是一个元素...只有元素有参数...
这是我的代码:
xercesc :: DOMNode * node = 0;
std :: string xml = from_an_obj_of_mine.GetXml(); / *一个带有xml的字符串,xml确定一个元素在* /
xercesc :: MemBufInputSource xml_buf((const XMLByte *)xml.c_str(),xml.size(),dummy) ;
xercesc :: XercesDOMParser * parser = new xercesc :: XercesDOMParser();
parser-> parse(xml_buf); / *解析器将包含从字符串中解析好的DOMDocument,我在这里得到要附加的节点* /
node = my_pointer_to_a_preexisting_domdocument-> GetXmlDocument() - > importNode(parser-> getDocument() > getDocumentElement(),true); / *我想将解析器中的节点附加到my_pointer_to_an_of_my_preexisting_domdocument的节点,它是一个不同的树,所以我必须导入节点以便以后附加* /
my_pointer_to_an_el_of_my_preexisting_domdocument-> appendChild(node);
如您所见,我想从字符串创建一个节点,我通过一个解析和那么需要导入节点来创建属于dom树的新的相同节点,我想附加新的节点。
我的步骤是:
-
获取xml字符串附加到一个预先存在的dom(存储为domdocument某个地方)
-
创建解析器
-
使用解析器从字符串
-
从我预先存在的dom(我要附加我的新节点),调用导入并克隆节点
,可以附加到预先存在的dom。 -
附加
问题是导入和导入会让我有一个节点...我想要一个元素来附加...
我使用appendChild来附加元素当然这个方法想要DOMNode *,但是给它一个DOMElement *(从DOMNode继承)就可以了...
如何获取元素从一个节点
删除wd_parser;
确定我发现了...
只需将节点重定向到元素即可完成... DOMNode是一个纯虚拟类,它是DOMElement的父类,所以它是正确的,它也是做事的方式(逻辑上说)
DOMElement * = dynamic_cast< DOMElement *>(node);
:)
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;
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);
:)
这篇关于在Xerces-C中从DOMNode *传递到DOMElement *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!