This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XPath issue in Java

 
Jhon Ortiz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part of it are you having trouble with?
 
Jhon Ortiz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic