2

我正在尝试解析 xml 文件,但在 try-catch 中收到此“可抛出消息”

这是什么意思&我应该怎么做?

此消息:http: //i.stack.imgur.com/1cHHa.jpg

java.lang.ClassCastException:org.harmony.xml.dom.ElementImpl$ElementAttrNamedNodeMapImpl 无法转换为 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 文件...... ...................................

<?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>

4

2 回答 2

0

修复你的进口。大概是这样的:

import android.renderscript.Element;

而对于 XML 解析应该有:

import org.w3c.dom.Element;

(或者android.sax.Element,如果您使用的是 SAX 解析器。)

于 2013-09-09T11:31:55.993 回答
0
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; 
于 2013-09-21T12:15:06.143 回答