wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XPath issue in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath issue in Java" Watch "XPath issue in Java" New topic
Author

XPath issue in Java

Jhon Ortiz
Greenhorn

Joined: May 28, 2009
Posts: 16
Hi,

I have the next XML document: http://dl.dropbox.com/u/19952564/docs/yweather-bogota.xml

And I need to extract attribute values from:



i.e: Tue, 9, 17, Cloudy, 26.

I have the following Java code:



But it does not print anything.

In http://www.xpathtester.com/test I paste the XML document, and it prints the results I expect for //yweather:forecast[1]/@* XPath expression.

Can you give some suggestions about this case.

Java SDK's XPath version is correct?

Thanks in advance for your response.

So long.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't see where you told the XPathExpression what namespace the "yweather" prefix represents. You have to provide the XPath object with a NamespaceContext.

(By the way, in English: "I have the following Java code".)
Jhon Ortiz
Greenhorn

Joined: May 28, 2009
Posts: 16
Sorry, Paul Clapham, I don't know how to accomplish that task.

Can you guide me or give me some ideas to accomplish that task?

Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What part of it are you having trouble with?
Jhon Ortiz
Greenhorn

Joined: May 28, 2009
Posts: 16
Paul Clapham,

Can you show me a simple example about extract attribute values from an XML element like: <yweather:forecast day="Tue" date="24 Apr 2012" low="9" high="17" text="Cloudy" code="26" />

Or you can give me some ideas or suggestions to do that using XPath from Java?

Thanks in advance for your invaluable help!

So long.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't understand. You already have a simple example, all you have to do to that example is to create a NamespaceContext object and assign it to the XPath object by calling the method which I believe is called setNamespaceContext(). That is the only idea or suggestion that you need.
Jhon Ortiz
Greenhorn

Joined: May 28, 2009
Posts: 16
I have solved this (thanks Paul Chapham)

I have used this class (found in: http://goo.gl/Ko97M):



My own method now looks like this:






So long.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Excellent!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath issue in Java
 
Similar Threads
Java xml namespace problem while reading xml element
XML Entity reference
XPath
Retrieve values for a complicate XML file (Java)
How to update/insert/delete an element from an XML document