Java XML:ClassCastException DeferredTextImpl [英] Java XML: ClassCastException DeferredTextImpl

查看:136
本文介绍了Java XML:ClassCastException DeferredTextImpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

// get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

    // Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();

// parse using builder to get DOM representation of the XML file
    dom = db.parse(file);

} catch (ParserConfigurationException pce) {
    pce.printStackTrace();
} catch (SAXException se) {
    se.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

NodeList n1 = dom.getChildNodes();
Element e1 = (Element) n1.item(0);

System.out.println(n1.getLength());
System.out.println(e1.getNodeName());

NodeList n2 = n1.item(0).getChildNodes();
Element e2 = (Element) n2.item(0);   //Line 61

System.out.println(n2.getLength());
System.out.println(e2.getNodeName());

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>

<test-fw:test
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test-fw="http://simitar/test-fw"> 

<rule-tree>
<rule class="matchlines">
<property name="contiguous"> true</property>
<property name="inOrder">false</property>
<property name="exact">false</property>
<property name="lines">modelInstantiated</property>
</rule>
<rule class="matchlines">
<property name="contiguous"> true</property>
<property name="inOrder">true</property>
<property name="exact">false</property>
<property name="lines">InitEvent</property>
</rule>
</rule-tree>

</test-fw:test>

这是我的输出:

1
test-fw:test
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at testpack.Main.run(Main.java:61)
    at testpack.Main.main(Main.java:86)

我不断收到此错误.我完全迷路了.我不知道该怎么做.我希望能够有一个节点,并能够抓住它的所有子节点,然后将它们放入数组或列表中,这样我就可以遍历它们.

I keep getting this error. I am completely lost. I have no idea what to do. I want to able to have one node, and be able to grab all it's children and put them into an array or list, so I can iterate through them.

这是我所有的进口商品:

Here are all my imports:

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Stack;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

尝试获取此Java来解析此XML文件时,我经历了最艰难的时光.

I've had the hardest time trying to get this Java to parse this XML file.

推荐答案

NodeList n1 = dom.getChildNodes();
Element e1 = (Element) n1.item(0);

该节点不是Element,而是Node.

尝试一下:

Node no1 = (Node) n1.item(0);

例如,节点可以是文本节点或元素.特别是

Nodes can be text nodes or elements, for example. In particular,

<root>
<element/>
</root>

4 个节点.一个root 元素,一个包含\n文本节点,一个element 元素和另一个文本节点包含\n.

is 4 nodes. A root element, a text node containing \n, the element element and another text node containing \n.

这篇关于Java XML:ClassCastException DeferredTextImpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆