how to read a specific element from multi level tags of xml(DOM) by using java

traitielsjalp

New Member
I want to read a multi level tags from xml(DOM) using java and the sample xml is : <root> <subclass> <subclass> <subclass> <name>test1</name> <address>address1</address> </subclass> <name>test2</name> <address>address2</address> </subclass> <name>test3</name> <address>address3</address> </subclass> </root> How to read <name>test2</name> and <address>address2</address> from the above xml?I have given a sample code .. but i need to find the values dynamically. when i am iterating bu using subclass tag, it's giving all the data. just i want to know how to get the specific data like <name>test2</name> and <address>address2</address> .Below is my java code which is reading the above xml:\[code\]NodeList fList = firstWordElement .getElementsByTagName("root"); for (int i = 0; i < fList.getLength(); i++) { Node firstFLNode = fList.item(i); if (firstFLNode.getNodeType() == Node.ELEMENT_NODE) { Element firstWdElement = (Element) firstFLNode; NodeList firstWdList = firstWdElement.getElementsByTagName("innerclass"); for (int j = 0; j < firstWdList.getLength(); j++) { Element firstWd1Element = (Element) firstWdList.item(j); if (firstWd1Element.getNodeType() == Node.ELEMENT_NODE) { String InnerName = ParseUtil.getTagValue("name", firstWd1Element); String InnerFormat = ParseUtil.getTagValue("format", firstWd1Element); String InnerDescription = ParseUtil.getTagValue("description", firstWd1Element); NodeList innerClassList = firstWd1Element.getElementsByTagName("subclass"); for (int k = 0; k < innerClassList.getLength(); k++) { Element subClassElement = (Element) innerClassList .item(k); if (subClassElement.getNodeType() == Node.ELEMENT_NODE) { String InnerSubName = ParseUtil.getTagValue("name", subClassElement); System.out.println("Innername==="+ InnerSubName); String InnerSubFormat = ParseUtil.getTagValue("format", subClassElement); System.out.println("Innerformat==="+ InnerSubFormat); String InnerSubDescription = ParseUtil.getTagValue("description", subClassElement); System.out.println("Innerdescription==="+ InnerSubDescription); } } } } } }\[/code\]
 
Back
Top