xml - Java xPath/DOM - When importing a node into a document, xPath query is unable to find new node (and sub node) -



xml - Java xPath/DOM - When importing a node into a document, xPath query is unable to find new node (and sub node) -

i trying import node 'new' dom document , execute xpath queries on newly imported nodes. xml i'm working has no namespaces. have followed standard procedure create document, shown:

document newdoc = null; documentbuilderfactory mill = documentbuilderfactory.newinstance(); seek { documentbuilder builder = factory.newdocumentbuilder(); newdoc = builder.newdocument(); } grab (parserconfigurationexception pce) { // parser specified options can't built pce.printstacktrace(); }

and import nodes...

// nodetoimport node prior xpath search (part of node set) node doctorunxpathon = newdoc.importnode(nodetoimport, true);

later, when seek run xpath query such as

xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); if (doctorunxpathon != null) { seek { xpathexpression expr = xpath.compile("//version_id/text()"); homecoming (string)expr.evaluate(doc, xpathconstants.string); } grab (xpathexpressionexception e) { logger.error(e); } }

the evaluation returns empty string. sample xml below:

<?xml version="1.0" encoding="utf-8"?> <version> <version_id>51312</version_id> <description>some description</description> </version>

the solution i've employed serialize dom string, , parse string re-build construction new dom. seems incredibly inefficient , backwards - missing import , xpath? had thought might because node's parent , such not beingness set properly, when did clone/adopt method (or adopting after importing) same behavior seen.

any answers/explanations/suggestions/thoughts appreciated.

i wrote little testcase reproduce, , after import xpath found "51312", maybe helps.

public class importnodetest { @test public void testimportnode() throws exception { final document newdocument = createdocument(); final element importedelement = getexistednode(); node doctorunxpathon = newdocument.importnode(importedelement, true); newdocument.appendchild(doctorunxpathon); xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); xpathexpression expr = xpath.compile("//version_id/text()"); system.out.println(expr.evaluate(newdocument, xpathconstants.string)); } private document createdocument() { document newdoc = null; documentbuilderfactory mill = documentbuilderfactory.newinstance(); seek { documentbuilder builder = factory.newdocumentbuilder(); newdoc = builder.newdocument(); } grab (parserconfigurationexception pce) { // parser specified options can't built pce.printstacktrace(); } homecoming newdoc; } private element getexistednode() { documentbuilderfactory mill = documentbuilderfactory.newinstance(); seek { documentbuilder builder = factory.newdocumentbuilder(); document document = builder.parse(new file("xml.xml")); homecoming document.getdocumentelement(); } grab (exception pce) { throw new runtimeexception(pce); } }

}

java xml dom xpath

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -