| Author |
How to get the value of a tag in xml using java
|
Raaja Gotluru
Ranch Hand
Joined: Mar 02, 2010
Posts: 84
|
|
Hi,
I am having an xml like below
I want the values of tags servername and theatername. Rest I dont want. How to get these using java. Is it possible to get the value using tagnames.
Thanks.
--raaja.g
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You can use XPath for that; both the interface and more generally the technique.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Raaja Gotluru
Ranch Hand
Joined: Mar 02, 2010
Posts: 84
|
|
Thanks for the reply. The problem is solved. I used dom parser to acieve this.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Why do you use a NodeList bookslist and not just doc.getDocumentElement()? Other than that, and the lack of error checking, that code looks decent.
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
I dont know if XPath is more proficient but looks more readable. please use code tag henceforth.
|
 |
Raaja Gotluru
Ranch Hand
Joined: Mar 02, 2010
Posts: 84
|
|
Hi,
Thanks for the reply. But the problem is how to get the root node at run time. Because the root node differes from xml to xml. Only servername and theatername are common.
If I use
xmlData += (xpath.evaluate("/Movies/servername", doc.getDocumentElement())).trim();
it will give problem as the root node differs.
Can we get the root node at run time. Please guide me.
Thanking you.
--raaja.g
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
That's what's being returned by doc.getDocumentElement(). Each XML document has only one single root element. Here it's called the document element.
|
 |
 |
|
|
subject: How to get the value of a tag in xml using java
|
|
|