0

I want to search an attribute value that exists in an XML file using XPath, for example to search in the nodes values you type "//ex1/ex2/ex3/text()" but what if i want to get the value of a parameter exists in a node,

let's consider a want to get humidity from the following XML data:

<?xml version="1.0" encoding="UTF-8"?> -<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <!-- api6.weather.ch1.yahoo.com Thu May 16 06:04:56 PST 2013 --> -<channel> <language>en-us</language> <ttl>60</ttl> <yweather:atmosphere pressure="29.97" visibility="3.73" rising="0" humidity="73"/> <yweather:astronomy sunset="6:41 pm" sunrise="4:59 am"/> </channel> </rss> 
1
  • Do you mean "attribute", instead of "argument" or "parameter"? Commented May 18, 2013 at 3:36

2 Answers 2

2

You could use //*/@humidity. That would give you all humidity attribute nodes, everywhere in the document.

Sign up to request clarification or add additional context in comments.

Comments

0

You would use //atmosphere/@humidity. GO through this XPath syntax tutorial.

7 Comments

That won't work, since <atmosphere> is in the yweather namespace.
but when i use it like this i have no errors but also have no returned result
XPathExpression expr = xpath.compile("//ttl/text()"); Object result = expr.evaluate(doc,XPathConstants.STRING); System.out.println(result);
@BrassBlock: no error because it's syntactically correct, but no result because no element matches //atmosphere. There is an element that matches //yweather:atmosphere, but you have to declare the namespace prefix in order to use that form.
@LarsH dude. i just gave an example of how to retrieve the attribute values using xpath. I could have made sure that namespace etc everything works but it is unnecessary noise. as even //yweather:atmoshpere wouldn't work without declaring the prefix and let xpath engine aware of this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.