Search code examples
androidxml-parsingdomparser

Issue in DOM XML Parsing in ICS(works fine in GingerBread)


I have trapped in some strange issue and I am not able to figure it out that what i am doing wrong. I have one screen where UserInfo is being displayed.For that i am getting the XML when i fired the webservice and i parse that data and display it which works well in 2.3.3 but failed to parse in ICS .

Here is my way to parse the data ::

XML ::

<?xml version="1.0"?>
<root>
 <displayname>AAAA</displayname>
 <gender>male</gender>
 <city>AAAA</city>
 <state>AAAA</state>
 <country>AAA</country>
 <image>http://www.example.com/documentSetting/test.jpg</image>
</root>

My AsyncTask ::

private class GetUserInfoTask extends AsyncTask<String, Integer, Document> 
{
    Document document = null;

    GetUserInfoTask()
    {
        webAPIRequest = new WebAPIRequest();
    }

    @Override
    protected Document doInBackground(String... urls) 
    {
        document = webAPIRequest.performGet(urls[0]);

        return document;
    }

    protected void onPostExecute(Document result) 
    {
        try
        {
            if(result!=null)
            {
                Element node = (Element)result.getElementsByTagName("root").item(0);

                if(node!=null)
                {
                    websiteList = node.getElementsByTagName("root");
                    check = websiteList.getLength();

                    if(check!=0)
                    {
                        arrUserInfoList=null;
                        if(arrUserInfoList==null)
                        {
                            arrUserInfoList = new ArrayList<UserProfile>();
                        }

                        for(int j=0;j<websiteList.getLength();j++)

                        {                               
                            Element checkNode=(Element)websiteList.item(j);
                            UserProfile user=new UserProfile(); 

                            msg.display_name = getValueFromNode(checkNode,"displayname");
                            msg.gender = getValueFromNode(checkNode,"gender");
                            user.city = getValueFromNode(checkNode,"city");
                            user.state = getValueFromNode(checkNode,"state");
                            user.country = getValueFromNode(checkNode,"country");
                            user.image = getValueFromNode(checkNode,"image");

                            arrUserInfoList.add(user);
                        }

                        txt_user_name_top.setText(arrUserInfoList.get(0).displayname);
                        txt_user_gender.setText(arrUserInfoList.get(0).gender);
                        txt_user_status.setText(arrUserInfoList.get(0).status);
                        txt_user_country.setText(arrUserInfoList.get(0).country);
                        txt_user_country.setText(arrUserInfoList.get(0).city);

                    }

                }
                new LoadingFriendImagesTask().execute();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
 }

My PerformGet Method ::

public Document performGet(String url) 
{
    Log.d("webAPIRequest url = ", url);
    Document doc = null;
    try 
    {
        DefaultHttpClient client = new DefaultHttpClient();

        URI uri = new URI(url);
        HttpGet method = new HttpGet(uri);
        HttpResponse res = client.execute(method);
        InputStream data = res.getEntity().getContent();
        String s =  convertStreamToString(data);
        ByteArrayInputStream str=new ByteArrayInputStream(s.getBytes());
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        doc = db.parse(str);

    }
    catch (ClientProtocolException e) 
    {
         e.printStackTrace();
    }   
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    catch (URISyntaxException e)
    {
        e.printStackTrace();
    } 
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();
    } 
    catch (SAXException e) 
    {
        e.printStackTrace();
    }

    return doc;
}

Your valuable suggestions and help will be appreciated ... Thanks in Advance ... :)


Solution

  • I have solved my problem like this.

    I have replaced this code ...

    else
    {
    node = (Element)result.getElementsByTagName("root").item(0);
    
    if(node!=null)
    {
        websiteList = node.getElementsByTagName("root");
        check = websiteList.getLength();
    
        if(check!=0)
        {
            if(arrUserInfoList==null)
            {
                arrUserInfoList = new ArrayList<UserProfile>();
            }
    
            for(int j=0;j<websiteList.getLength();j++)
            {                           
            }
        }
    }
    }
    

    with this ::

    else
    {
    websiteList = (NodeList)document.getElementsByTagName("root");
    check = websiteList.getLength();
    
    if(check!=0)
    {
        if(arrUserInfoList==null)
        {
            arrUserInfoList = new ArrayList<UserProfile>();
        }
    
        for(int j=0;j<websiteList.getLength();j++)
        {
        }
    }
    }
    

    This gonna worked for me like a charm .. :)