0

I'm having a little trouble parsing a string of xml called responseText in android. The xml is fully valid and has the following structure:

<plan>
<entry>
<name>john</name>
<address>uk</address>
</entry>
<entry>
<name>joe</name>
<address>usa</address>
</entry>
</plan>

The code I am using to parse the String is as follows:

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();

        is.setCharacterStream(new StringReader(responseText));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("entry");

        for (int i = 0; i < nodes.getLength(); i++) {

            Element element = (Element) nodes.item(i);

            NodeList name = ((Document) element)
                    .getElementsByTagName("name");
            Element line = (Element) name.item(0);
            Toast.makeText(Containers.this,
                    getCharacterDataFromElement(line), Toast.LENGTH_SHORT)
                    .show();

            NodeList title = ((Document) element)
                    .getElementsByTagName("address");
            line = (Element) title.item(0);
            Toast.makeText(Containers.this,
                    getCharacterDataFromElement(line), Toast.LENGTH_SHORT)
                    .show();

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

public static String getCharacterDataFromElement(Element e) {
    Node child = ((Node) e).getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "?";

}

I'm just using simple toasts to print the xml data to screen. However, i get an IOexception when I enter the for loop. Any idea what is wrong?

3
  • Please post the full text of the IOException. Commented Feb 2, 2011 at 21:44
  • How do I get the full text? It may not actually be an Ioexception - I do know however that it enters an exception, I was just looking at the logcat thing and the ioexception may have been to do with an error on my phone. Where does the stacktrace print to? Or how would I enable it? Commented Feb 2, 2011 at 21:47
  • To help you with the error, the full text of the exception would be nice. Anyway, you should use this for parsing XML: androidpeople.com/android-xml-parsing-tutorial-using-saxparser Commented Feb 2, 2011 at 21:51

3 Answers 3

8

Are you importing the types from the correct packages? Something like

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.sax.Element; // Wrong Element class

Change the last import to

import org.w3c.dom.Element;

and try again.

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

2 Comments

This was the exact problem - the wrong import. No idea how I managed that! Thanks.
wow! Eclipse auto-imports can be REALLY dum at times... thanks a ton.. had the exact same problem
1

I don't see anything that could cause an IOException inside the loop.

However, are you sure you can just go and cast an Element into a Document? At any rate you shouldn't need to anyways, since Element also has the getElementsByTagName method.

9 Comments

If I don't cast it eclipse underlines it as an error, not sure why.
@Nicklas: But what does the actual compiler say...?
@Matti Virkkunen The method getElementsByTagName(String) is undefined for the type Element
@Nicklas: Let me just confirm, we are talking about this class, right?
@Nicklas: You can't fix the problem by using a cast to Document, unless the node actually is a Document. And your element is not a Document. This may or may not be the cause for the exception that you're seeing, but it is the cause of an exception, whether or not you've gotten there yet.
|
0

Try adding an xml declaration at the start of your string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.