Java:XML Parsing

KaxExporgoVar

New Member
I have the xml file as follows,\[code\]<CHPkt xmlns="Smartscript/EmarSchema"> <CHInfo> <StoreId>1800</StoreId> <CHId>DB439A79-3D6F-4D25-BE0A-C4692978C072</CHId> <CHName>Test</CHName> <Address> <Address1>Test Address</Address1> </Address> <DrugRounds> <RoundTime>09:00</RoundTime> <RoundTime>13:00</RoundTime> <RoundTime>17:00</RoundTime> </DrugRounds> </CHInfo></CHPkt>\[/code\]How to get the values of the tags which has the same name, my code is as follows,\[code\]public class ReadXml { public static void main(String[] args){ try{ File xmlFile = new File("/home/jayakumar/Desktop/SmartScript.XML"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlFile); NodeList nodeList1 = document.getElementsByTagName("CHInfo"); System.out.println("######################################"); for(int i =0;i<nodeList1.getLength();i++){ org.w3c.dom.Node node = nodeList1.item(i); if(node.getNodeType()== org.w3c.dom.Node.ELEMENT_NODE){ Element element = (Element) node; System.out.println("StoreId : " + getTagValue("StoreId", element)); System.out.println("CHId : " + getTagValue("CHId", element)); System.out.println("CHName : " + getTagValue("CHName", element)); System.out.println("Address : " + getTagValue("Address1", element)); } NodeList nodeList2 = document.getElementsByTagName("DrugRounds"); System.out.println("-------------->"+"DrugRounds"); for(int j =0;j<nodeList2.getLength();j++){ org.w3c.dom.Node subNode = nodeList2 .item(j); Element e = (Element) subNode; System.out.println("RoundTime : "+getTagValue("RoundTime", e)); System.out.println("RoundTime : "+getTagValue("RoundTime", e)); System.out.println("RoundTime : "+getTagValue("RoundTime", e)); } } }catch (Exception e) { System.out.println(e.getMessage()); } } private static String getTagValue(String sTag, Element element) { NodeList nlList = element.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = http://stackoverflow.com/questions/10845562/(Node) nlList.item(0); return nValue.getNodeValue(); } }\[/code\]I wasn't able to extract the values of second and third Roudtimes.How to parse the tags with same nameThanks
 
Back
Top