This code snippet
explains about the parsing of the xml file using the DOM parser.
import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XMLParser { /** * @param args * @throws IOException * @throws SAXException */ public void GetXMLValue(String FileName){ try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); File file = new File(FileName); if(file.exists()){ Document doc = null; try { doc = db.parse(file); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element docelement1 = doc.getDocumentElement(); System.out.println("Root element of the document: " + docelement1.getNodeName()); //for commercial credit NodeList valueList1 = docelement1.getElementsByTagName("CommercialCredit"); if (valueList1 != null && valueList1.getLength() > 0) { for (int i = 0; i < valueList1.getLength(); i++) { Node node = valueList1.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; NodeList nodeList = e.getElementsByTagName("DistSourceId"); System.out.println("DistSourceId: " + nodeList.item(0).getChildNodes().item(0) .getNodeValue()); nodeList = e.getElementsByTagName("MMSReferenceStartTime"); System.out.println("MMSReferenceStartTime: " + nodeList.item(0).getChildNodes().item(0) .getNodeValue()); nodeList = e.getElementsByTagName("MMSReferenceEndTime"); System.out.println("MMSReferenceEndTime: " + nodeList.item(0).getChildNodes().item(0) .getNodeValue()); } // for Credit result ---- added only 3 fields to test..... NodeList valueList2 = docelement1.getElementsByTagName("CreditResult"); if (valueList2 != null && valueList2.getLength() > 0) { for (int j = 0; j < valueList2.getLength(); j++) { Element e2 = (Element) node; NodeList nodeList2 = e2.getElementsByTagName("MediaAssetId"); System.out.println("MediaAssetId: " + nodeList2.item(0).getChildNodes().item(0) .getNodeValue()); nodeList2 = e2.getElementsByTagName("DetectedTime"); System.out.println("DetectedTime: " + nodeList2.item(0).getChildNodes().item(0) .getNodeValue()); nodeList2 = e2.getElementsByTagName("DetectedSourceId"); System.out.println("DetectedSourceId: " + nodeList2.item(0).getChildNodes().item(0) .getNodeValue()); } }//if ends here } } else{ System.exit(1); } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main (String[] args) throws SAXException, IOException { // TODO Auto-generated method stub String str = "C:\Test\Mytestfile.xml"; new XMLParser().GetXMLValue(str); } }