Cannot read the ChildNode

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Cannot read the ChildNode

Postby mobilenet » Thu Dec 02, 2010 11:22 am

Hi everyone, good day.
I need some help on below coding.

I have a XML below and trying to use the DOM method to read it. However when I read the ChildNode always give me null value.
Code: Select all
<?xml version='1.0' encoding='UTF-8'?>
<DocumentElement>
  <CUSTOMER>
    <no>1</no>
    <cust_code>112345</cust_code>
    <cust_name>CCC</cust_name>
  </CUSTOMER>
  <CUSTOMER>
    <no>1</no>
    <cust_code>345345345</cust_code>
    <cust_name>YYY</cust_name>
  </CUSTOMER>
  <CUSTOMER>
    <no>1</no>
    <cust_code>4353345</cust_code>
    <cust_name>KKK</cust_name>
  </CUSTOMER>
  <CUSTOMER>
    <no>4</no>
    <cust_code>FFFFFF</cust_code>
    <cust_name>CHAN YAO KANG</cust_name>
  </CUSTOMER>   
</DocumentElement>

Code: Select all
   
private void ReadXml() throws XmlPullParserException, IOException,
         SAXException, ParserConfigurationException {
      String name = null;

      File fXML = new File("/data/data/sfa_android.com/files/Cust.xml");

      Document dom = parseXMLFile(fXML);
      org.w3c.dom.Element root = dom.getDocumentElement();
      NodeList items = root.getElementsByTagName("DocumentElement");

      for (int i = 0; i < items.getLength(); i++) {
         Node item = items.item(i);

         NodeList properties = (NodeList) item.getChildNodes();

         for (int j = 0; j < properties.getLength(); j++) {
            Node property = properties.item(j);
            //Cannot get the NodeName
            name = property.getNodeName();
            
            //name = properties.item(j).getFirstChild().getNodeValue();
         }
      }
      tvStatus.setText(name.toString());
   }

protected Document parseXMLFile(File file)
         throws ParserConfigurationException, SAXException, IOException {
      Document songXml = null;
      try {
         DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
               .newDocumentBuilder();
         InputStream fis = new FileInputStream(file);
         songXml = docBuilder.parse(fis);
         return songXml;
      } catch (FileNotFoundException e) {

      }
      return songXml;
   }
mobilenet
Freshman
Freshman
 
Posts: 2
Joined: Sun Nov 28, 2010 10:01 am

Top

Return to General

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests