November 10th, 2011, 05:57 AM
|
#1 (permalink)
|
|
New Member
Join Date: Oct 2011
Location: Co. Mayo, Ireland
Posts: 10
Device(s): Vodafone 858 Smart
Thanks: 0
Thanked 0 Times in 0 Posts
|
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;
}
|
|
|
Last edited by Droid_Coder; November 10th, 2011 at 06:07 AM.
|
|