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


I am attempting to import a node into the 'new' dome document and then execute the xpath query on the newly imported nodes I do The XML I am working with is not the namespace I have followed the standard procedure for document creation, as shown:

  document newDoc = null; Document Filter Factory Factory = Document Builder. Try {DocumentBuilder Builder = factory.newDocumentBuilder (); NewDoc = Builder. New document (); } Hold (Parser Configuration Exception Piece) {// Parser pce.printStackTrace () can not be made with specified options; }   

To import more nodes ...

  // nodeToImport is a node from a pre xPath search (part of the node set) node docToRunXPathOn = NewDoc.importNode (NodeTimePort, true);   

Later, when I try to run the xpath query such as

  XPathFactory xPathFactory = XPathFactory.newInstance (); XPath xpath = xPathFactory.newXPath (); If (docToRunXPathOn! = Null) {try {XPathExpression expr = xpath.compile ("// version_id / text ()"); Return (string) expr.evaluate (doctor, XPathConstants.STRING); } Catch (XPathExpressionException e) {logger.error (e); }}   

The evaluation returns an empty string. A sample XML is below:

  & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Version & gt; & Lt; Version_id & gt; 51,312 & lt; / Version_id & gt; & Lt; Details & gt; Some details & lt; / Description & gt; & Lt; / Edition & gt;   

The solution I employ is to sort a DOM into a string, and then parse it immediately to rebuild the structure as a new DOM. It seems incredibly inefficient and backwards - what am I missing from this import and exposed? I thought it could be because the parents of the node and they are not being set properly, but when I had adopted the method of cloning / adoption (or after import), then only one behavior was seen. is.

Any answers / explanations / suggestions / ideas appreciated.

I wrote short testcases to reproduce, and after the import, XPath got "51312" , Maybe it helps.

  public class ImportNodeTest {@Test public void testImportNode () throws an exception {last document newDocument = createDocument (); Last element imported element = getExistedNode (); Node docToRunXPathOn = newDocument.importNode (imported element, true); NewDocument.appendChild (docToRunXPathOn); XPathFactory xPathFactory = XPathFactory.newInstance (); XPath xpath = xPathFactory.newXPath (); XPathExpression expr = xpath.compile ("// version_id / text ()"); System.out.println (expr.evaluate (new document, XPathConstants.STRING)); } Private Document Making Document () {document newDoc = null; Document Filter Factory Factory = Document Builder. Try {DocumentBuilder Builder = factory.newDocumentBuilder (); NewDoc = Builder. New document (); } Grip (parser configuration exception pitch) {// pars pce.printStackTrace () with specific options can not be made; } New dock; } Private Element getExistedNode () {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); Try {DocumentBuilder Builder = factory.newDocumentBuilder (); Document document = builder.parse (new file ("xml.xml")); Return Document. GetDocumentElement (); } Hold (exception PSE) {new runtime exception (piece); }}   

}

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -