My goal : to get the root of the XML in a Node object , and then evaluate it !
My problem :
I’m trying to evaluate my expression from the ROOT of the XML file, I have this method ( I need to implement is) :
public Object evaluate(String expression, QName returnType);
Assume that I’ve already opened the XML with Document , like this :
//load the document into a DOM Document
this.domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
this.builder = domFactory.newDocumentBuilder();
this.doc = builder.parse("books.xml");
//create an XPath factory
this.factory = XPathFactory.newInstance();
//create an XPath Object
this.xpath = factory.newXPath();
Now when I do this , inside public Object evaluate(String expression, QName returnType);
:
String unitedString = " my characters " ;
rootNode = doc.getChildNodes().item(0);
System.out.println(rootNode.getNodeName()); // this line presents the name of the root
Object returnedObject= xpath.evaluate(unitedString,rootNode ,returnType); // this line makes eclipse go crazy
Eclispe says after line “4” : " DTMManagerDefault.getDTMHandleFromNode(Node) line: not available "
But in line “3” , Eclipse produces the name of the root , which is inventory …
So where did I go wrong ?
What’s wrong with it ?
Thank you all , Jack
first:
Your unitedString is not a valid xPath expression, it should be something like /root/node/ xPath’s describe paths to (a) specific node(s) to in your xml file.
second:
The root node of any xml is a special node called the DocumentElement, you can get to it by calling doc.getDocumentElement()