Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merging doms

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
suppose I have created a dom like the one below
------------------------------------------
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<field><fieldname>field1</fieldname><visible>t</visible></field>
--------------------------------------------

and I want to merge it with another dom with the above dom as a child ie the expected output should be like
------------------------------------------
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<blk>
<name> block </name>
<field><fieldname>field1</fieldname><visible>t</visible></field>
</blk>
--------------------------------------------
I'd tried using appendchild(child dom) and importNode(child dom, true) on the child dom but keeps getting error.

any suggestions? maybe there is some API that can perform this operation?
 
I suggest you take a look at the excellent book &quot;Java and XML&quot; by Brett McLaughlin, and look for the 'dreaded wrong document exception' explanation. In short, you must first import the node, which creates a new node compatible with the target. Something like the following should work:
Code:
Element blk   = blkDoc.getDocumentElement();
Element field = fieldDoc.getDocumentElement();
Element imported = (Element)blkDoc.importNode( field );
blk.appendChild( imported );
Hope this helped. Cheers, Neil :)
 
Damn,
I skimmed through your question, and didn't spot that you had already tried importNode(). Sorry :-(. Hope you manage to figure it out. Perhaps if you posted the code snippet?
Neil
 
for the record, i managed to solve it. I'd used importNode wrongly. the correct code is as follows

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document thedom = db.newDocument();

Element starttag = thedom.createElement(&quot;blk&quot;);
//...codes for appending the text

starttag.appendChild(thedom.importNode(fielddom.getDocumentElement(), true));
//assuming fielddom is in existent and is well-structured

thans to Neil
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top