开发者

Passing from a DOMNode* to a DOMElement* in Xerces-C

开发者 https://www.devze.com 2023-02-05 12:04 出处:网络
I have a c++ application that manipulates xml. Well, at a certain point of my开发者_JAVA技巧 application I get a DOMNode* and then I attach it to an element as a child.

I have a c++ application that manipulates xml. Well, at a certain point of my开发者_JAVA技巧 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);

:)

0

精彩评论

暂无评论...
验证码 换一张
取 消