Get-value-from-xml-using-getElementsByTagName

This page describes how to get the contents of a element using getElementsByTagName from an xml file.

Suppose we want the get the values from the elements id, firstName and lastName from the xml:

Below an example on how to do this:

How we do this:

First we create a instance of Document Object and parse the xml using the below code:

Because we want to get the value of id, firstName and lastName for all students we get the element student to iterate later over the returned NodeList:

Next we iterate over the nodes object and cast each node to an Element object. Foreach element call getElementsByTagName(“name”).item(0).getTextContent() to get the value.

Sources xml-dom-get-elements-by-tag-name

Leave a Reply

Your email address will not be published. Required fields are marked *