jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes How to get the value of a tag in xml using java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to get the value of a tag in xml using java" Watch "How to get the value of a tag in xml using java" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to get the value of a tag in xml using java
 
Similar Threads
Call javascript function from Flash Form
writing a file into a remote server
request.getServerName behaviour Help Needed
Not able to delete xml files after reading
please help me with this app - URGENT !