Search code examples
javaandroidxmldomsax

Parse xml file in android ,fetch all attibutes of given node


I am trying to get the id, name and pin from the below posted xml file and i wrote the below posted code. Problem is, this line node.getNodeValue() return always null

please let me know how to get the id, name and pin from the xml file correctly?

code

try {
            URL url = new URL(link);
            URLConnection conn = url.openConnection();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(conn.getInputStream());

            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("employee");

            Log.i(TAG, " nodeLength:" + nodes.getLength());


            for (int i = 0; i < nodes.getLength(); i ++) {
                Node node = nodes.item(i);
                Log.i(TAG, "Current Element :" + node.getNodeValue());//THIS LINE
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

xml

<employee_list>
<employee id="21358" pin="0000" name="www"/>
<employee id="21359" pin="0011" name="qqq"/>
<employee id="20752" pin="1011" name="Test"/>
<employee id="814" pin="1012" name="Pf"/>
<employee id="21372" pin="1013" name="Kru"/>
</employee_list>

stack trace

01-24 15:05:54.219 5624-5718/com.example. I/XmlParser:  nodeLength:33
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example.I/XmlParser: Current Element :null
01-24 15:05:54.220 5624-5718/com.example. I/XmlParser: Current Element :null

Solution

  • You can fetch the NamedNodeMap of all Attributes and use getNamedItem with Id,pin,name as key to this function and finally fetch the value using getNodeValue function

         NodeList nodes = doc.getElementsByTagName("employee");
         for (int i = 0; i < nodes.getLength(); i ++) {
             Node node = nodes.item(i);
             NamedNodeMap attr =  node.getAttributes();
    
             // find item using key name and then fetch the node value
             String id = attr.getNamedItem("id").getNodeValue();
             String pin = attr.getNamedItem("pin").getNodeValue();
             String name = attr.getNamedItem("name").getNodeValue();    
             System.out.println(id+" "+pin+" "+name);
         }
    

    Output :

    21358 0000 www
    21359 0011 qqq
    20752 1011 Test
    814 1012 Pf
    21372 1013 Kru
    

    or you can also use the loop instead of fetching single values

         NodeList nodes = doc.getElementsByTagName("employee");
         for (int i = 0; i < nodes.getLength(); i ++) {
             Node node = nodes.item(i);
             NamedNodeMap attr =  node.getAttributes();
             for(int j = 0 ; j<attr.getLength() ; j++) {
                 Attr attribute = (Attr)attr.item(j);     
                 System.out.println("Current Element :" + attribute.getName()+" = "+attribute.getValue());
              } 
         }