This code snippet
explains about the parsing of the xml file using the DOM parser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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); } } |