Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to parse xml file in android?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 173
    Comment on it

    If you are looking to parse an xml file in android here are its steps :-

    Step 1:-Getting XML content by making HTTP request

    public String getXmlFromUrl(String url)
      {
        String xml = null;
         try
           {
             DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpPost httpPost = new HttpPost(url);
             HttpResponse httpResponse = httpClient.execute(httpPost);
             HttpEntity httpEntity = httpResponse.getEntity();
              xml = EntityUtils.toString(httpEntity);
            } 
        catch (UnsupportedEncodingException e)
           {
             e.printStackTrace();
            } 
        catch (ClientProtocolException e)
           {
             e.printStackTrace();
            } 
        catch (IOException e) 
          {
            e.printStackTrace();
           }
          return xml;
        }
    

    step 2:- Parsing XML content and getting DOM element of xml.

    public Document getDomElement(String xml)
     {
      Document document = null;
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        try 
         {
           DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            document = documentBuilder.parse(is);
           } 
         catch (Exception e)
           {
           Log.e("Error: ", e.getMessage());
           return null;
           }
        return document;
      }
    

    Step 3:- Get each xml child element value by passing element node name.

     public String getValue(Element item, String str)
      {     
        NodeList nodeList = item.getElementsByTagName(str);       
         return this.getElementValue(nodeList.item(0));
       }
     public final String getElementValue( Node nodeElement )
      {
         Node child;
         if( nodeElement != null)
          {
             if (nodeElement.hasChildNodes())
               {
                   for( child = nodeElement.getFirstChild(); child != null; child = child.getNextSibling() )
                     {
                        if( child.getNodeType() == Node.TEXT_NODE  )
                          {
                             return child.getNodeValue();
                          }
                     }
                  }
              }
                 return "";
          } 
    

    Step:-4 Code in your main activity

    String NODE_NAME="item";
    String CHILD_ONE="id"
    String CHILD_TWO="name"
    ArrayList<String> itemList = new ArrayList<String>();
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document document = parser.getDomElement(xml); // getting DOM element
    NodeList nodeList = document.getElementsByTagName(NODE_NAME);
    for (int i = 0; i < nodeList.getLength(); i++)
       {
          Element element = (Element) nodeList.item(i);
           String itemid=parser.getValue(element,CHILD_ONE)) // 1st child value of node
           String itemName=parser.getValue(element,CHILD_TWO))//2nd child value of node
           String myString=itemName+" has id "+itemid;
           itemList.add(myString);
         }
    

    Now you have the list of childItems , you can use them in your own way.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: