jQuery in Action, 2nd edition*
The moose likes XML and Related Technologies and the fly likes Parsing XML file using Xpath in jdk1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML file using Xpath in jdk1.4" Watch "Parsing XML file using Xpath in jdk1.4" New topic
Author

Parsing XML file using Xpath in jdk1.4

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430


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").

Thanks
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

You need to include third party libraries for XML parsing if you are using JDK 1.4...


Java Experience
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41134
    
  45
Sunny Bhandari wrote:You need to include third party libraries for XML parsing if you are using JDK 1.4...

Not XML parsing libraries as such (Java 1.4 has those), just an XPath library - like Jaxen.


Ping & DNS - my free Android networking tools app
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Is it not possible to parse XML in 1.4?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41134
    
  45
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.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok thanks Ulf. I'll have a look at Jaxen.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41134
    
  45
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.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok i am trying this with JDOM which i believe uses Jaxen.

I am trying to parse the same xml file using the following code



it is failing with a classcast exception at Element elem = (Element) iter.next();

Any ideas?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

The cast exception is because the xpath expression is asking for the text() node not element node.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41134
    
  45
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.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I dont actually need the element object i just need to extract the content of the element i am trying to exctract. I changed it to something like this



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41134
    
  45
So all is good now?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Yes - All thanks to you!
Thanks for the help.
vtd-xml author
Greenhorn

Joined: Mar 04, 2011
Posts: 1
FYI, the xpath code in vtd-xml (http://vtd-xml.sf.net/)

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing XML file using Xpath in jdk1.4
 
Similar Threads
XPath Problem
XPath Problem
Performance Recommendation for Simple XML Parsing
XPath Problem
Problem applying an XPath expression to an org.w3c.dom.Node