How to parse xml tag using DOM parser in java?

Thajefekak

New Member
I am going to parse following xml file in java using DOM parser but not getting the information to retrieve some tag from my xml file so please help me out for this.\[code\]<policy name="DayCapturePolicy" type="capture"><starttime>9:00:00 AM</starttime><endtime>9:00:00 PM</endtime><event name="NEW_APP_INSTALLED"> <eventFilter selectedAppName="Alarm"/> <eventParam>selectedAppName</eventParam> <eventParam>selectedAppVersion</eventParam> <eventParam>appPackageName</eventParam> <eventParam>appDirPath</eventParam></event></policy>\[/code\]I want to know how should i parse the value for event filter tag from above xml file?I also want to know how should i retrieve all the values from eventParam tag.I am able to retrieve value for first tag i.e.selectedAppName but unable to parse the value for salectedAppVersion.appPackageName,appDirPath.Thanks In advanceI had tried this code for parsing.\[code\] NodeList nodeList = document.getElementsByTagName("policy"); Log.d("Reminder", "Number of Policies : "+nodeList.getLength()); for(int i=0;i<nodeList.getLength();i++){ Log.d("Reminder", "In 1"); //PolicyInfo policyInfo = new PolicyInfo(); Node node = nodeList.item(i); NamedNodeMap namedNodeMap = node.getAttributes(); String policyName = namedNodeMap.getNamedItem("name").getNodeValue(); String policyType = namedNodeMap.getNamedItem("type").getNodeValue(); if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node; Log.d("startTime : " ,""+ getTagValue("starttime", eElement)); Log.d("endtime : " ,""+ getTagValue("endtime", eElement)); NodeList nList = document.getElementsByTagName("event"); for(int j=0;i<nList.getLength();i++){ Log.d("Reminder", "In 1"); //PolicyInfo policyInfo = new PolicyInfo(); Node innerNode = nList.item(i); NamedNodeMap innerNamedNodeMap = innerNode.getAttributes(); String eventName = innerNamedNodeMap.getNamedItem("name").getNodeValue(); Log.d("innerNode", eventName); if(innerNode.getNodeType()== Node.ELEMENT_NODE){ Element innerElement = (Element) innerNode; Log.d("eventParam : " ,""+ getTagValue("eventParam", innerElement)); Log.d("eventFilter : " ,""+ getTagValue("eventFilter", innerElement)); //Log.d("endtime : " ,""+ getTagValue("endtime", eElement)); } //System.out.println("starttime : " + getTagValue("starttime", eElement)); // System.out.println("endtime: " + getTagValue("endtime", eElement)); //System.out.println("Nick Name : " + getTagValue("nickname", eElement)); //System.out.println("Salary : " + getTagValue("salary", eElement)); } }\[/code\]
 
Back
Top