• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Reading a value from XML node.

Droid_Coder

Newbie
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;
        }
 
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.
 
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.
 
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.
 
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...
 
Back
Top Bottom