0

i'll try to read an xml, following a tutorial finded online here http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

public String leggi_palinsesto()
{
    String comodo="";
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    // looping through all item nodes <item>

    for (int i = 0; i < nl.getLength(); i++) {
        //Element e = null;
        Element e = (Element) nl.item(i);
        String titolo = parser.getValue(e, KEY_TITOLO); // name child value
        String artista = parser.getValue(e, KEY_ARTISTA); // cost child value
        System.out.println(artista+" - "+titolo);
        comodo=artista+" - "+titolo;
    }

    return comodo;
}

When the debug arrive to Element e = (Element) nl.item(i); return classcastexception error, but I'm sure that's the correct code.

This is the Logcat

07-06 18:03:14.561: E/AndroidRuntime(1602): FATAL EXCEPTION: main
07-06 18:03:14.561: E/AndroidRuntime(1602): java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
07-06 18:03:14.561: E/AndroidRuntime(1602):     at it.axiomatic.radioamicizia.RadioAmiciziaActivity.leggi_palinsesto(RadioAmiciziaActivity.java:254)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at it.axiomatic.radioamicizia.RadioAmiciziaActivity$1$1.run(RadioAmiciziaActivity.java:121)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at android.os.Handler.handleCallback(Handler.java:587)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at android.os.Looper.loop(Looper.java:123)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at java.lang.reflect.Method.invoke(Method.java:521)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-06 18:03:14.561: E/AndroidRuntime(1602):     at dalvik.system.NativeStart.main(Native Method)

This is the .xml file:

http://www.radioamicizia.com/demo.xml

EDIT: I've tried with original example xml file (http://api.androidhive.info/pizza/?format=xml), but at casting of node to element... ERROR!

EDIT:

static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITOLO = "name";
static final String KEY_ARTISTA = "cost";

Any suggestion?

2
  • debug to the exact line. and attach the error log stacktrace Commented Jul 6, 2012 at 15:55
  • I dont know what do you want I trace, but the problem is when I try to casting the node like element, because the node contain information. :( Commented Jul 7, 2012 at 5:11

1 Answer 1

2

From Java documentation:

public Node item(int index)

NodeList.item(i) returns a Node not an Element. The node can be anything else not just an Element and that explains why you are getting that error.

If you want to process only elements of your XML, you should do this check before:

if(nl.item(i).getNodeType() == Node.ELEMENT_NODE){
  Element e = (Element) nl.item(i);
  // Process element here
}

EDIT Replace your for loop with this one:

for (int i = 0; i < nl.getLength(); i++) {
    if(nl.item(i).getNodeType() != Node.ELEMENT_NODE)
            continue;

    Element e = (Element) nl.item(i);
    String titolo = parser.getValue(e, KEY_TITOLO); // name child value
    String artista = parser.getValue(e, KEY_ARTISTA); // cost child value
    System.out.println(artista+" - "+titolo);
    comodo=artista+" - "+titolo;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Tried, but at first check enter in the condition and at the assign return me the error. Maybe something wrong in xml?
Did you replace the whole for loop? See my edit for an update
:( The cast of the node to element generate always same error... :( :(
Can you show us the value of the KEY_ITEM, KEY_TITOLO and KEY_ARTISTA values?
Sure, I'm edited. But i dont think this is the problem, because NodeList nl = doc.getElementsByTagName(KEY_ITEM); dont creates problem, but only when i try to cast crash.

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.