2

I found the following easy solution to extracting values from XML file.

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("c:/temp/books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//book[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()); } } } 

This uses xpath to extract all books title where the author is Neal Stephenson from the following xml

<inventory> <book year="2000"> <title>Snow Crash</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>Burning Tower</title> <author>Larry Niven</author> <author>Jerry Pournelle</author> <publisher>Pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> </book> <book year="1995"> <title>Zodiac</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> </book> <!-- more books... --> </inventory> 

Now this works fine on JDK5 but i am using jdk 1.4 Can this be converted to the java 1.4 equivalent?

All i am trying to do is extract a value from an xml element. For example, in the above xml, i just want something that is the equivalent of getElementByTag("title").

Thanks

1
  • +1 for Neal Stephenson (I could not resist :P)
    – mantrid
    CommentedMar 7, 2013 at 2:51

1 Answer 1

2

Quick google resulted in links like this and this which confirm that JAXP can be downloaded separately and run on top of JDK 1.4.2. You might run into configuration problems as mentioned in the apache link. Good luck!

5
  • Is it not possible without external downloads?
    – ziggy
    CommentedFeb 26, 2011 at 18:05
  • Sorry mate i am slightly confused. I thought JAXP is already in 1.4. See this esus.com/javaindex/j2se/jdk1.2/jaxp/jaxp.html is the DOM parser in 1.4 not part of JAXP?
    – ziggy
    CommentedFeb 26, 2011 at 22:08
  • JAXP is just an interface. You also need an implementation, and the XPath implementation javax.xml.xpath was introduced in JDK 5. That being said, you can download implementations for 1.4.CommentedFeb 26, 2011 at 22:36
  • Hi mark, Please see jaxp.java.net/1.3/Updating.html#java-14 towards the end of the document it says that an implementation of JAXP is built in to the jdk1.4. I dont quite understand the difference in the downloadable implementation and the implementation included in jdk1.4 and also why i cant access the built in implementation
    – ziggy
    CommentedFeb 27, 2011 at 11:41
  • I thought you wanted XPath support. Xpath support is available in JAXP from 1.3 I believe. Whatever is built in Jdk 1.4 is JAXP 1.1 which would not have Xpath support.CommentedMar 2, 2011 at 17:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.