-1

我已经失去了两个小时试图解析 XML,而且我惊慌失措。希望你们能帮助我。

我有一个像这样的 XML 文件:

<document src="xmls/sections/master/37_11.xml">
<name>Somename</name>
<list show="grid">
    <filter>
        <option id="1">A</option>
        <option id="2">B</option>
        <option id="3">C</option>
        <option id="4">D</option>
        <option id="5">E</option>
        <option id="6">F</option>
    </filter>
    <item id="3" detail="true">
        <filter>
            <value id="1">A</value>
        </filter>
        <logo src="media/img/" alt=""></logo>
        <name>this is a name</name>
        <description>content</description>
        <icons></icons>
    </item>
    <item id="3" detail="true">
        <filter>
           <value id="1">A</value>
        </filter>
        <logo src="media/img/" alt=""></logo>
        <name>this is a name</name>
        <description>content</description>
        <icons></icons>
    </item>
</list>
</document>

所以我在Java中有这个解析器:

this.contingut = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(contingut);
doc.getDocumentElement().normalize();

NodeList nodes = doc.getElementsByTagName("list");
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    Element element = (Element) node;
    if(element.getNodeType() == Element.ELEMENT_NODE){
        //section.setProductType(getValueInt("product_type", element));
        NodeList child = element.getChildNodes();
        //fem el parsing de la galeria d'elements de list:
        Log.d("debugging","Tipus de mostrar?"+element.getAttribute("show")); //THIS IS WORKING
        NodeList filtersTags = element.getElementsByTagName("filter"); 
        Node listNode = filtersTags.item(0);
        Element listElement = (Element) listNode; 
        if(listElement.getNodeType() == Element.ELEMENT_NODE){
             NodeList fillsList = listElement.getChildNodes(){
             Log.d("debugging","QttFills listElement: "+fillsList.getLength());
            for (int k=0; k<fillsList.getLength();k++){//donem voltes als fills de list.
            Node fillList = fillsList.item(k);
            Element elementList = (Element) fillList;
            if(elementList.getNodeType() == Element.ELEMENT_NODE){
                 if (elementList.getNodeName().equals("filter")){
                Log.d("debugging","Filter!!!");
                 } else if (elementList.getNodeName().equals("i")){
                        Log.d("debugging","item!!!");
                 }
            }
        }
    }
}

我得到

java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl 无法转换为 org.w3c.dom.Element On:

Element elementList = (Element) fillList;

你们能帮帮我吗?我不明白为什么会崩溃...

非常感谢。

4

2 回答 2

2

这就是我让你的代码做你想做的事情的方式:

for (int k=0; k<fillsList.getLength();k++){//donem voltes als fills de list.
    Node fillList = fillsList.item(k);
    if (fillList instanceof Element) {
        Element elementList = (Element) fillList;
        if(elementList.getNodeType() == Element.ELEMENT_NODE) {
            if (elementList.getNodeName().equals("filter")){
               System.out.println("Filter!!!");
            } else if (elementList.getNodeName().equals("option")){
               System.out.println("item!!!");
            }
        }
    }
}
于 2013-05-16T08:28:20.227 回答
0

从我找到的文档中,TextImpl只实现了Node接口,而不是Element接口。所以这里的代码:

Node fillList = fillsList.item(k);
Element elementList = (Element) fillList;

fillList类型为时会失败TextImpl。请注意,这发生您的两个if测试之间。

于 2013-05-16T08:21:27.383 回答