• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Parsing XML file using Xpath in jdk1.4

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to include third party libraries for XML parsing if you are using JDK 1.4...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it not possible to parse XML in 1.4?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks Ulf. I'll have a look at Jaxen.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 https://coderanch.com/how-to/java/XmlLinks page which links to even more stuff.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cast exception is because the xpath expression is asking for the text() node not element node.

Thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So all is good now?
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - All thanks to you!
Thanks for the help.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI, the xpath code in vtd-xml (http://vtd-xml.sf.net/)

 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic