Parsing an XML file and getting info from it via XpathFrom WikiJavabuy this book This article shows how to open an XML file and extract data from it using Xpath. The example uses only libraries which are contained in the standard API (JDK 6). so no additional libraries are required.
the article
before retrieving information from an XML file using Xpath you must create a org.w3c.dom.Document out of it. this is in the example below done by the code below. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory .newDocumentBuilder(); Document doc = builder.parse(file);
This is basically opening the stream containing the XML file and parsing it into the org.w3c.dom.Document object which basically represents the whole XML into the memory like a tree. Once you have the Document object containing your XML in a tree structure you could explore it manually like a normal tree. Or you can use the great power of Xpath to extract data from the Document. XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(query); NodeSet nodeSetResult = (NodeSet) expr.evaluate(doc, XPathConstants.NODESET); The snippet above obtains from the XPathFactory factory an XPath object. The XPath object is then initialized via the compile(String) method. Successively the evaluate(Document, String) is called, in order to execute the Xpath Query in the Document that was loaded before. the second argument of the evaluate(Document, String) method represents the expected result type of the query. It is important to remember that the method returns an Object that needs to be explicitly casted. XMLReader.javapackage org.wikijava.examples.xpath; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.sun.org.apache.xpath.internal.NodeSet; public class XMLReader { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { if (args.length < 2) { throw new Exception("error, check the command line"); } String file = args[1]; String query = args[2]; List<String> resultList = new ArrayList<String>(); try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = documentBuilderFactory .newDocumentBuilder(); Document doc = builder.parse(file); if (doc == null) { throw new Exception("unable to load Document"); } XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(query); NodeSet nodeSetResult = (NodeSet) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodeSetResult.size(); i++) { String result = nodeSetResult.item(i).getTextContent(); resultList.add(result); } } catch (XPathExpressionException e) { throw new Exception(e); } catch (SAXException e) { throw new Exception(e); } catch (IOException e) { throw new Exception(e); } catch (ParserConfigurationException e) { throw new Exception(e); } } } |

When i try to compile this code I get this error:
C:\Documents and Settings\...\Desktop>javac XMLReader.java XMLReader.java:20: warning: com.sun.org.apache.xpath.internal.NodeSet is Sun proprietary API and may be removed in a future release import com.sun.org.apache.xpath.internal.NodeSet; ^ XMLReader.java:54: warning: com.sun.org.apache.xpath.internal.NodeSet is Sun pro prietary API and may be removed in a future release NodeSet nodeSetResult = (NodeSet) expr.evaluate(doc, ^ XMLReader.java:54: warning: com.sun.org.apache.xpath.internal.NodeSet is Sun pro prietary API and may be removed in a future release NodeSet nodeSetResult = (NodeSet) expr.evaluate(doc, ^ 3 warningsWhat Could I Do ?
--78.13.58.249 09:54, 16 December 2008 (UTC)