I am trying to parse some xml from a remote api, the xml is formed like this:
<django-objects version="1.0">
<object pk="13" model="ballot.poll">
<field type="CharField" name="question">wwwww</field>
<field type="DecimalField" name="budget">1</field>
<field type="CharField" name="option1"></field>
<field type="CharField" name="option2"></field>
<field type="CharField" name="option3"></field>
<field type="CharField" name="option4"></field>
<field type="CharField" name="pollType">YesNo</field>
<field type="DateField" name="startDate">2013-05-17</field>
<field type="DateField" name="endDate">2013-05-17</field>
<field type="CharField" name="targetGender">M</field>
<field type="CharField" name="targetMarital">All</field>
<field type="SmallIntegerField" name="targetMinAge">1</field>
<field type="SmallIntegerField" name="targetMaxAge">1</field>
</object>
</django-objects>
Here is my parsing code, it crashes when trying to output the nodevaluewith a "println needs message" exception
InputStream is = new ByteArrayInputStream(msg.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document dom = null;
try {
dom = builder.parse(is);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch blockk
e.printStackTrace();
}
org.w3c.dom.Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("field");
**Log.d("number of fields: ",items.getLength());** // this outputs 13 as expected because there are 13 field elements
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
**Log.d("field", item.getNodeValue());** //crash here
}
item.getNodeValue()is returning null. Since this is XML have you considered using org.xmlpull.v1.XmlPullParser as described in developer.android.com/training/basics/network-ops/xml.html