How to read an attribute tag with an equals sign from XML in Java?
Question
I'm parsing an XML file through Java, and am able to parse through Nodes in the XML file that appear as:
<name><given>familyName</given></name>
by using code such as the snippet shown below:
NodeList givenElmntLst = firstElement.getElementsByTagName("given");
Element givenNmElmnt = (Element) givenElmntLst.item(0);
NodeList fstNm = givenNmElmnt.getChildNodes();
String given = ((Node) fstNm.item(0)).getNodeValue();
but I am unable to read attributes that are formatted as such:
<birthTime value="19230101"/>
How should I be reading values such as the one above differently? Thanks in advance.
Solution
i'm assuming your using the org.w3c.dom
package...
try something like this:
NodeList birthTimeLst = firstElement.getElementsByTagName("birthTime");
Element birthTime = (Element) birthTimeLst.item(0);
String value = birthTime.getAttribute("value");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow