Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old November 10th, 2011, 05:57 AM   #1 (permalink)
New Member
 
Droid_Coder's Avatar
 
Join Date: Oct 2011
Location: Co. Mayo, Ireland
Posts: 10
 
Device(s): Vodafone 858 Smart
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading a value from XML node.

I'm writing an app that makes an http request to my website and it returns a XML result set that looks like the following:

<ResultSet version="1.0">
<Status>
<code>0</code>
<message>Success</message>
<found>1</found>
</Status>
<Result>
<id>1115550003</id>
<distance>0.00</distance>
<compatibility>100</compatibility>
</Result>
</ResultSet>

I'm trying to read the <code> value but I'm having no luck. I can read the <message> value successfully and place that into a private string variable, but when I try to read the <code> value in the same manner, the getNodeValue() call is returning null. Below is my code...


Code:
    public boolean xmlGetStatus(String response)
        {
        try
            {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(new StringReader(response)));

            NodeList resultNodes = doc.getElementsByTagName("Status");           
            Node resultNode = resultNodes.item(0);           
            NodeList attrsList = resultNode.getChildNodes();

            boolean result = false;
            for (int i=0; i < attrsList.getLength(); i++)
                {
                Node node = attrsList.item(i);               
                Node firstChild = node.getFirstChild();
                // Check the error code and return True or False
                if ("code".equalsIgnoreCase(node.getNodeName()) && firstChild!=null)
                    {
                    if ("0".equalsIgnoreCase(firstChild.getNodeValue())
                        {
                        result = true;
                        }
                    }
                // Also, stuff the ErrorMessage into a variable we can use later if need be.
                if ("message".equalsIgnoreCase(node.getNodeName()) && firstChild!=null)
                    {
                    sXmlError = firstChild.getNodeValue();
                    }
                }

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

        return false;
        }

Droid_Coder is offline  
Last edited by Droid_Coder; November 10th, 2011 at 06:07 AM.
Reply With Quote
Sponsors
Old November 10th, 2011, 06:15 AM   #2 (permalink)
New Member
 
Droid_Coder's Avatar
 
Join Date: Oct 2011
Location: Co. Mayo, Ireland
Posts: 10
 
Device(s): Vodafone 858 Smart
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually the posted code does work now. I found a typo in the code and once I fixed it I got the results I expected.

However, I found I that to get this parsing of the <code> value to work I needed to treat the value as a string and use the following code to compare the value.

Code:
                    if ("0".equalsIgnoreCase(firstChild.getNodeValue()))
                        {
                        result = true;
                        }
What I wanted to do was to was to convert to an Integer and then check the value. But using Integer.getInteger(firstChild.getNodeValue()) resulted in a null pointer exception and I'm not sure why.
Droid_Coder is offline  
Reply With Quote
Old November 11th, 2011, 12:04 AM   #3 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

I'm a bit rustly on the XML DOM. I remember that element nodes don't have a value and that you need to get the value of the text node that will be the child of the element node.
jiminaus is offline  
Reply With Quote
Old November 15th, 2011, 04:28 AM   #4 (permalink)
New Member
 
Droid_Coder's Avatar
 
Join Date: Oct 2011
Location: Co. Mayo, Ireland
Posts: 10
 
Device(s): Vodafone 858 Smart
Thanks: 0
Thanked 0 Times in 0 Posts
Default Integer.parseInt() worked

Thanks for the input. Turns out the only change I needed to make was to use Integer.parseInt instead of Integer.getInteger. This allowed me to convert the value from the XML file to an actual Integer value.
Droid_Coder is offline  
Reply With Quote
Old November 15th, 2011, 12:28 PM   #5 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

Integer.parseInt and Integer.valueOf should both work.

valueOf works for almost any object you need to convert to. So like String.valueOf(Object) etc...
alostpacket is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:13 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo