I’m trying to read XML file, ex :
<entry>
<title>FEED TITLE</title>
<id>5467sdad98787ad3149878sasda</id>
<tempi type="application/xml">
<conento xmlns="http://mydomainname.com/xsd/radiofeed.xsd" madeIn="USA" />
</tempi>
</entry>
Here is the code I have so far: http://pastebin.com/huKP4KED
Here is what I need, concerning XML above:
- I need to get value of title, id
- attribute value of tempi as well as madeIn attribute value of contento
What is the best way to do this ?
Edit
@Pascal Thivent
Maybe creating method would be good idea like public String getValue(String xml, Element elementname), where you specify tag name, the method returns tag value or tag attribute (maybe give it name as additional method argument) if the value is not available
What I really want to get certain tag value or attribute if tag value(s) is not available, so I’m in the process of thinking what is the best way to do so since I’ve never done it before.
The best solution for this is to use XPath. Let’s say we have the following
feed.xmlfile:Here’s a short but compile-and-runnable proof-of-concept (with
feed.xmlfile in the same directory).This produces the following output:
Note how using XPath makes the value retrieval very simple, intuitive, readable, and straightforward, and "missing" values are also gracefully handled.
API links
package javax.xml.xpath