简体   繁体   中英

xml parsing exception

i'm trying to parse a xml file but in try-catch receive this "throwable message"

what mean this & what i should do?

this mesage : http://i.stack.imgur.com/1cHHa.jpg

java.lang.ClassCastException: org.harmony.xml.dom.ElementImpl$ElementAttrNamedNodeMapImpl cannot be cast to android.renderscript.Element

  try 
    {   
        InputStream in=getResources().openRawResource(R.raw.words);
        DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();  

        Document doc=builder.parse(in, null);       
        Log.i("problem detect", "Line Detecting");
        NodeList words=doc.getElementsByTagName("word");        


            items.add((Element) ((DocumentBuilderFactory)words.item(1)).getAttribute("words"));

            //items.add((Element)words.item(1).getAttributes());


        in.close();

    }
    catch (Throwable t) 
    {
        new AlertDialog.Builder(this).setTitle("Catch").setMessage(t.toString()).show();

...........................................XML File......................................

<?xml version="1.0" encoding="utf-8"?>
<words> 
    <word value="lorem" />
    <word value="ipsum" />
    <word value="dolor" />
    <word value="sit" />
    <word value="amet" />
    <word value="consectetuer" />
    <word value="adipiscing" />
    <word value="elit" />
    <word value="morbi" />
    <word value="vel" />
    <word value="ligula" />
    <word value="vitae" />
    <word value="arcu" />
    <word value="aliquet" />
    <word value="mollis" />
    <word value="etiam" />
    <word value="vel" />
    <word value="erat" />
    <word value="placerat" />
    <word value="ante" />
    <word value="porttitor" />
    <word value="sodales" />
    <word value="pellentesque" />
    <word value="augue" />
    <word value="purus" />

</words>

Fix your imports. There's probably something like:

import android.renderscript.Element;

while for XML parsing there should be:

import org.w3c.dom.Element;

(Or android.sax.Element if you're on a SAX parser.)

try {  

    DocumentBuilder db = dbf.newDocumentBuilder();  

    InputSource is = new InputSource();  
        is.setCharacterStream(new StringReader(xml));  
        doc = db.parse(is);   

    } catch (ParserConfigurationException e) {  
        Log.e("Error: ", e.getMessage());  
        return null;  
    } catch (SAXException e) {  
        Log.e("Error: ", e.getMessage());  
        return null;  
    } catch (IOException e) {  
        Log.e("Error: ", e.getMessage());  
        return null;  
    }  

    return doc; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM