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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to merge n DOM trees into a new DOM tree? 1

Status
Not open for further replies.

BTCMan

Programmer
Jun 17, 2005
21
BR
Hi people,

I have a List object with "n" different DOM trees (each element of the List object has a Document object).
I want to create a new DOM tree object and insert in it each one of the "n" DOM trees I have in the saved List. So the final result will be a single DOM.
Any hint of code to perform this merge? Thanks in advance. Cheers,

BTCMan
 
I solved this issue. Code below for reference:

Initial state:
operDom -> Document object with a DOM tree
-------------------------------------------
public Document appendDoms(Document operDom) {
Document joinDom = null;

// Creates a new DOM tree to hold the join result
joinDom = createDom();

if (joinDom == null) {
return null;
}

// Creates a root Element Tag for the new DOM tree
org.w3c.dom.Element root = (org.w3c.dom.Element) joinDom.createElement("JoinRoot");
joinDom.appendChild(root);

//append in the new DOM the operDOM object
appendNode(operDom, joinDom);

return joinDom;
}


// Auxiliary methods:
public Document createDom () {
Document doc = null;
DocumentBuilder docBuilder = null;

try
{
DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
docBuildFactory.setValidating(false);
docBuilder = docBuildFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
}
catch (ParserConfigurationException e1) {
return null;
}
return doc;
}


public void appendNode(Document doc1, Document doc2) {
// Gets the root Element for doc1
org.w3c.dom.Element root1 = doc1.getDocumentElement();

Node importedNode = doc2.importNode(root1, true);

// Gets the root Element for doc2 and adds in
// doc2's root Element the doc1's child
org.w3c.dom.Element root2 = doc2.getDocumentElement();
root2.appendChild(importedNode);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top