title: Java XML Parsing of complex xml file
body: <p>I really hope you can help me on this one.... 
  I need to get specific data from my xml file but I am stuck at one point and I can't figure out how to proceed...</p>
  
  <p>I want to get from Network: network name; From Codes: the mcc and mnc code; From Settings:name, id, type, parameter name and value;</p>
  
  <p>So this is the structure of my xml file:</p>
  
  <pre><code>&lt;country country="Andorra" isoCode="AD"&gt;
      &lt;networks&gt;
          &lt;network name="Mobiland" isMNO="true" ranking="10"&gt;
              &lt;codes&gt;
                  &lt;code mcc="213" mnc="03" /&gt;
              &lt;/codes&gt;
              &lt;settings&gt;
                  &lt;setting alternativeName="Mobiland AD" ref="s1" name="IAP" id="2266" /&gt;
                  &lt;setting alternativeName="Mobiland MMS AD" ref="s2" name="MMS" id="2265" /&gt;
              &lt;/settings&gt;
          &lt;/network&gt;
      &lt;/networks&gt;
      &lt;settings&gt;
          &lt;setting id="s2" type="mmssetting"&gt;
              &lt;parameter name="mms-gprs-access-point-name" value="MMS" /&gt;
              &lt;parameter name="mms-gprs-name" value="MMS" /&gt;
              &lt;parameter name="mms-gprs-proxy" value="192.168.021.050" /&gt;
              &lt;parameter name="mms-gprs-proxy-port" value="9201" /&gt;
              &lt;parameter name="mms-url" value="http://mms.ad/mmsc" /&gt;
          &lt;/setting&gt;
          &lt;setting id="s1" type="iapsetting"&gt;
              &lt;parameter name="iap-gprs-access-point-name" value="internet" /&gt;
              &lt;parameter name="iap-gprs-name" value="Internet" /&gt;
              &lt;parameter name="iap-gprs-url" value="http://google.com" /&gt;
          &lt;/setting&gt;
      &lt;/settings&gt;
  &lt;/country&gt;
  </code></pre>
  
  <p>This is what I have so far...... and I really can't proceed any further... I have a null pointer exception at String content = cNode.getLastChild().getTextContent().trim(); and I am not sure that I am doing it the right way.....</p>
  
  <p>public void ReadXML() throws ParserConfigurationException, SAXException,
              IOException {</p>
  
  <pre><code>    // Get the DOM Builder Factory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
      // Get the DOM Builder
      DocumentBuilder builder = factory.newDocumentBuilder();
  
      // Load and Parse the XML document
      // document contains the complete XML as a Tree.
      Document document = builder.parse("D:\\test.xml");
  
      List&lt;Apn&gt; empList = new ArrayList&lt;Apn&gt;();
  
      // Iterating through the nodes and extracting the data.
      NodeList nodeList = document.getDocumentElement().getChildNodes();
  
      for (int i = 0; i &lt; nodeList.getLength(); i++) {
  
          Node node = nodeList.item(i);
          if (node instanceof Element) {
              //Apn apn = new Apn();
              System.out.println(node.getAttributes().getNamedItem("country")
                      .getNodeValue());
  
              NodeList childNodes = node.getChildNodes();
              for (int j = 0; j &lt; childNodes.getLength(); j++) {
                  Node cNode = childNodes.item(j);
  
  
                  if (cNode instanceof Element) {
                      String content = cNode.getLastChild().getTextContent()
                              .trim();
  
                      switch (cNode.getNodeName()) {
                      case "networks":
                          System.out.println(content);
                          break;
                      case "setting":
                          System.out.println(content);
                          break;
                      case "codes":
                          System.out.println(content);
                          // emp.location = content;
                          break;
                      }
                  }
              }
  
          }
  
      }
  }
  </code></pre>
  
  <p>Any help will be appericiated !</p>
  
  <p>Thanks!</p>