Uniting NodeLists in DOM (java) -
here's code:
private nodelist union(nodelist left, nodelist right){ nodelist result=null; try{ documentbuilderfactory domfactory = documentbuilderfactory.newinstance(); domfactory.setnamespaceaware(true); // never forget this! documentbuilder newbuilder = domfactory.newdocumentbuilder(); document newdoc = newbuilder.newdocument(); element root = newdoc.createelement("root"); newdoc.appendchild(root); if(left!=null){ for(int i=0;i<left.getlength();i++){ node tmp=(node)left.item(i).clonenode(true); newdoc.adoptnode(tmp); newdoc.getdocumentelement().appendchild(tmp); //root.appendchild(newdoc.importnode((node)left.item(i), true)); } } if(right!=null){ for(int i=0;i<right.getlength();i++){ node tmp=(node)right.item(i).clonenode(true); newdoc.adoptnode(tmp); newdoc.getdocumentelement().appendchild(tmp); //root.appendchild(newdoc.importnode((node)right.item(i), true)); } } result=root.getchildnodes(); } catch(parserconfigurationexception e){ system.err.println(e); } return result; }
in code i'm trying unite 2 nodelists one.
it works well, except fact after union, nodes loses context of parent, ancestor, preceding-sibling etc... if i'm trying run evaluate on result , use parent/ansector/preceding-sibling/etc axis on result, fails.
what should in order won't lose it?
thanks.
a node can exist in 1 document. if want copied node in both documents you're out of luck. can create new node in target document , move children , attributes old node new node. @ document::adoptnode(node) might easiest way that.
Comments
Post a Comment