3

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;

John Watts
  • 8,717
  • 1
  • 31
  • 35
Andry
  • 16,172
  • 27
  • 138
  • 246

1 Answers1

7

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);

:)

Andry
  • 16,172
  • 27
  • 138
  • 246
  • 6
    Hi, actually you should check the type of the node beforehand with getNodeType(). If it is an ELEMENT_NODE you can cast it to DOMElement. Learn more at: http://xerces.apache.org/xerces-c/apiDocs-3/classDOMNode.html – Clemens Jul 08 '12 at 18:04