1

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

}
3
  • Can you post the exception or cause of the crash you mention? Commented May 17, 2013 at 21:37
  • added error, its "println needs a message", like theres nothing being returned by .getNodeValue() Commented May 17, 2013 at 21:41
  • You're saying that 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 Commented May 17, 2013 at 21:44

2 Answers 2

2

A 'element' node contains a number of child nodes of different types. You want the 'text' node which is always the first child, so you can access it like this...

item.getFirstChild().getNodeValue()

Or it's more straightforward in Java 1.5 and above as you can call getTextContent() directly.

Sign up to request clarification or add additional context in comments.

Comments

1

Cast the node to an element.

Element tmpElmnt = (Element) item;

Then you can use the appropriate methods from Element class to serve your purpose. You will get detailed documentation of Element class here,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.