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.
I am trying to understand parsing in XML but it is quite difficult. The problem i have is that i am working with JDK1.4 and i am trying to parse an xml file using xpath.
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)
Joined: Mar 22, 2005
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)
The cast exception is because the xpath expression is asking for the text() node not element node.
Joined: Mar 22, 2005
Element is just one of several object types selectNodes can return. You either need to cast to a specific one (if you're sure that all nodes will be of that type), or test the type using instanceof before casting. I would recommend the latter approach, combined with logging that reports unexpected types.