繁体   English   中英

xml解析异常

[英]xml parsing exception

我正在尝试解析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>

修复您的导入。 可能是这样的:

import android.renderscript.Element;

而对于XML解析,应该有:

import org.w3c.dom.Element;

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

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; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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