I found the following easy solution to extracting values from XML file.
This uses xpath to extract all books title where the author is Neal Stephenson from the following xml
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").
O. Ziggy wrote:Is it not possible to parse XML in 1.4?
Of course it is - Java 1.4 has full support for DOM and SAX built in. What it does not have built in is support for XPath (that was introduced in Java 5). That shouldn't be a surprise since XPath was finalized after the Java 1.4 API was finalized. Jaxen does a fine job of implementing XPath, although -obviously- with an API other than javax.xml.xpath.
But Java 1.4 was marked obsolete quite a few years ago, so it's to be expected that these days you need to go to some hassle to support it.
Having looked around i cant work out whether xpath is possible in JDK1.4 or not.
I must admit i dont quite understand the whole XML processing architectures. There seems to be quite a lot of libraries, API that is resulting in my brain being overloaded.
From what i have read, to be able to use XPATH i need JAXP. Some articles say that JAXP is included in JDK1.4 and some say it is not. is it?
I tried running the following code and it didnt work in 1.4 but does work in 1.5
Having read further i found that JAXP can be downloaded separately. But the problem i found is that there are so many variants of it.
I did download one of the variants, i.e. http://jaxp.java.net/downloads.html. This gave me to library files
I tried the same code with the above libraries in the class path and it seems to have compile successfully but when i run it it just terminates.
I guess i need to take a step back and understand what i am doing.
- What do i need to do to be able to parse an xml file using xpath *in jdk1.4*
- Is there a resource that can help in understanding xml processing (i.e. the difference in JAXP, JDOM, XPATH, xalan, xerces etc)
What do i need to do to be able to parse an xml file using xpath *in jdk1.4*
As I said, the Jaxen library can do that, but it has its own API - NOT javax.xml.xpath.
The JAXP package you downloaded can be made to work with Java 1.4, but you need to resort to some trickery which I would not recommend for production systems. Jaxen really is the better choice.
If you insist on using the javax.xml.xpath API, then you need to convince your JVM to use those jar files instead of its own built-in classes. You can either a) copy jaxp-api.jar and jaxp.ri.jar to <java-home>/lib/endorsed where <java-home> refers to the directory where the runtime software is installed (which is the top-level directory of the J2SE Runtime Environment or the jre directory in the JDK), or b) if you prefer not to change the JRE, you can start the JVM with the -Djava.endorsed.dirs=%JAXP% switch, where JAXP should point to the directory that contains those jar files.
Is there a resource that can help in understanding xml processing (i.e. the difference in JAXP, JDOM, XPATH, xalan, xerces etc)
Start with the XmlFaq. It links to lots of good material, including introductory articles on the topics you asked about, and to the http://www.coderanch.com/how-to/java/XmlLinks page which links to even more stuff.