在android中使用XML解析的ClassCastException

时间:2013-07-16 19:42:19

标签: java android xml

我在Android环境中解析一些XML文件时遇到问题... 启动我的Android应用程序后,我在JAVA环境下进行了一些解析XML的测试。

所以,我写了这段代码:

private void addMeta(Node n){
    String[] fields = {"src", "timestamp", "title", "kind", "ref", "crcData"};
    Element e = (Element)n.getChildNodes();
    //... more code
}

但是,在Android中使用它时会返回一个ClassCastException。

我一直在寻找问题,并且(据我所知)问题如下:

Android正在获得另一种" NodeList" (" n.getChildNodes()"返回的数据类型.Android获取的数据类型是:org.apache.harmony.xml.dom.NodeListImpl。

所以,看到这一点,我改变了我的代码,从直接转换到执行它的方法。它的代码是:

private org.w3c.dom.Element getElements(Node n){
    org.w3c.dom.NodeList nl = (org.w3c.dom.NodeList)n.getChildNodes();
    return (org.w3c.dom.Element)nl;
}

然后,我改变"元素e =(元素)n.getChildNodes()" by" org.w3c.dom.Element e = this.getElements(n)"

但是,内部" getElements"方法,奇怪的事情发生......

" NL"仍然是" org.apache.harmony.xml.dom.NodeListImpl",所以我仍然得到一个ClassCastException。

所以,我可能需要得到一个" org.w3c.dom.NodeList"而不是" org.apache ......"节点列表?

谢谢大家,对不起我的英语不好......:s

2 个答案:

答案 0 :(得分:0)

NodeList指向0个或更多节点,因此无法直接转换为节点/元素。根据结构,子节点可以包括节点的元素和属性。

要从NodeList到达特定节点,您必须迭代它。

for (int i = 0; i < nodeList.getLength(); i++) {
   Node item = nodeList.item(i);
   // if this matches the element name you're 
   // looking for then return else continue
}

另一个选项是查看给定节点是否为元素,然后使用getElementsByTagName(“name”)进行查询,然后迭代它以找到正确的节点然后返回它。

if (node.getNodeType() == Node.ELEMENT_NODE) {
   Element e = (Element) node;
   NodeList elements = e.getElementsByTagName("name_of_tag_you_want");
   // process node list and find the right one and return

}

答案 1 :(得分:0)

感谢您的回答。

但是,在解析我的XML时,我正在注意始终使用子节点获取节点(因此,getChildNodes将始终返回NodeList)。

在调用“addMeta”之前,我使用一些函数迭代节点:

起点是“填充”方法:

public boolean fill(String[] curr, String file){
    try{
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new java.io.StringReader(file)));

        doc.getDocumentElement().normalize();

        for(int i=0;i<curr.length;i++)
            this.treat((doc.getElementsByTagName(curr[i])), curr[i]);
    }catch(ParserConfigurationException e){
        this.error = Strings.PARSEEXCEPTION.getValue();
        this.exception = e;
        return false;
    }catch(SAXException e){
        this.error = Strings.SAXEXCEPTION.getValue();
        this.exception = e;
        return false;
    }catch(IOException e){
        this.error = Strings.IOEXCEPTION.getValue();
        this.exception = e;
        return false;
    }

    return this.check();
}

然后,“治疗”方法:

private void treat(NodeList nl, String kind){
    for(int i=0;i<nl.getLength();i++)
        this.add(nl.item(i), kind);
}

“添加”方法:

private void add(Node n, String kind){
    if(kind.equals("meta"))
        this.addMeta(n);
            //... more code

而且,在“addMeta”中,我想要获得的所有物品都有孩子。我试图解析的XML的基本结构:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="myweb/myxmlns.xsd">
<meta>
    <generator>
        <src>62.83.204.127</src>
        <timestamp>1359743849</timestamp>
    </generator>
    <doc>
        <title>Title</title>
        <kind>m</kind>
    </doc>
    <ext>
        <authUser>
            <ref>David-Solé-González-1</ref>
        </authUser>
    </ext>
    <crcData>2e021a71461e5a1bf6b71a5779446857a1d6b073</crcData>
</meta>
</Document>

当然它在JAVA 中效果很好,但在android中却给出了ClassCastException ......