org.xml.sax.SAXParseException while parsing XMl using XPATH

RajenR

New Member
Am trying to get values from an XML using XPATH. I got the following exception.\[code\] [Fatal Error] books.xml:4:16: The prefix "abc" for element "abc:priority" is not bound.Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:///D:/XSL%20TEST%20APP%20BACK%20UP/XMLTestApp/books.xml; lineNumber: 4; columnNumber: 16; The prefix "abc" for element "abc:priority" is not bound. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at xpath.XPathExample.main(XPathExample.java:18)\[/code\]Am getting this error because my XML is a little bit of different from normal one.My XML file is follows.\[code\]<?xml version="1.0" encoding="UTF-8"?><inventory> <Sample> <abc:priority>1</abc:priority> <abc:value>2</abc:value> </Sample></inventory>\[/code\]Am using the following java code to get values from the above XML.\[code\]import java.io.IOException; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import javax.xml.xpath.*; public class XPathExample { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//Sample/*/text()");////book/Sample[author='Neal Stephenson']/title/text() Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } } }\[/code\]If i remove the semicolon i never get this error.Is it possible to get content from an XML like mentioned above using Xpath ?If so guide me to find out the solution.Any help is appreciableThanks.
 
Back
Top