Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value using getElementsByTagName

Tags:

java

xml

w3c

How to get the value of the tag name using getElementsByTagName. My Xml file is

<parent>
<method>name</method>
....
....
</parent>

Here i want to take the value of method alone. i used the following piece of code, but i am getting as object

File fXmlFile = new File(FILE_XML);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
doc.getElementsByTagName("method").toString();
like image 687
BKK Avatar asked Aug 14 '12 06:08

BKK


1 Answers

doc.getElementsByTagName("method") returns a NodeList.

You want the first one of these, so you should use doc.getElementsByTagName("method").item(0) - which returns a Node.

From this, you probably want the value; doc.getElementsByTagName("method").item(0).getTextContent() should get you that.

like image 96
Binil Thomas Avatar answered Nov 13 '22 00:11

Binil Thomas