Click here to Skip to main content
15,896,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string which contains the xml representation of an xml node which I intend to insert in a xml document loaded in memory. The xml string (of node)is something like this

XML
<ns1:Feature name=\"PageSize\">\
    <ns1:Option name=\"A4\" />\
 </ns1:Feature>


So , it has got namespace for the tag names as well.

Is there a way I can achieve this?

1)I tried to user XMLDomNode->put_text() , but it does not work as it replaces the "<" and ">" chars by their text representations (<etc.)

2) I was wondering if loading the string buffer in a separate in-memory xml document and then getting the node pointer from there will work on my original document. But again , not sure if the XMLDOMnodes are transferable within documents.
Posted

To import an XMLDOMNode from one xml to an other one, you have to use importNode from the IXMLDOMDocument3 interface.

you cannot add a node from an xml document inside an other document without importNode.

For example:
C++
MSXML2::IXMLDOMDocument3Ptr xml;
xml.CreateInstance(__uuidof(DOMDocument60));
MSXML2::IXMLDOMElementPtr elt = xml->createElement("root");
xml->appendChild(elt);

MSXML2::IXMLDOMDocument3Ptr subxml;
subxml.CreateInstance(__uuidof(DOMDocument60));
subxml->loadXML(fromString);

MSXML2::IXMLDOMNodePtr node = xml->importNode(subxml->GetdocumentElement(),VARIANT_TRUE);
elt->appendChild(node);


I did not check with subxml nodes that contains namespace.

DOMDocument60 is available when importing XML v6
C++
#import <msxml6.dll>
#include <msxml6.h>
#include <msxml.h>
 
Share this answer
 
v2
Comments
kaushik_code 17-Jul-15 9:24am    
Thanks for the answer , I just discovered the same solution myself. Thanks a lot for posting.
I solved this myself using the 2nd approach:

1) Create an in-memory xml document based on
IXMLDOMDocument3 
interface and load the xml string in there.

2) Select the node you require using the
selectNode()
method.

3) Now go back to your orinial xml document where you want the node placed and load it again as a IXMLDOMDocument3 interface.

4) Use the
C++
importNode()
method of IXMLDOMDocument3 from step 3 to clone the node obtained in step 2.

5) You can now use the cloned node to do an
appendChild()
to the original xml.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900