java.lang.ClassCastException, DeepNodeListImpl 不能被转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16970828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassCastException, DeepNodeListImpl cannot be cast
提问by zeptyan
here is my code :
这是我的代码:
public void Login() {
try{
DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = builderfactory.newDocumentBuilder();
File path = new File("src/dataPengguna/dataPengguna.xml");
Document doc = db.parse(path);
Element pengguna = (Element) doc.getElementsByTagName("pengguna");
NodeList list = pengguna.getElementsByTagName("user");
for (int i = 0; i < list.getLength(); i++) {
Element user = (Element) list.item(i);
Node username = user.getElementsByTagName("username").item(i);
Node password = user.getElementsByTagName("password").item(i);
if(loginuser.getText().equals(username.getTextContent())
&& loginpass.getText().equals(password.getTextContent())){
JOptionPane.showMessageDialog(rootPane, "welcome");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
here is my xml :
这是我的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<pengguna>
<user>
<nama>septian</nama>
<username>septiansykes</username>
<password>1234</password>
<status>belumpinjam</status>
</user>
<user>
<nama>koko</nama>
<username>kokosan</username>
<password>12er</password>
<status>belumpinjam</status>
</user>
<user>
<nama>tamrin</nama>
<username>tamrincs</username>
<password>gt234</password>
<status>belumpinjam</status>
</user>
</pengguna>
and here is my error :
这是我的错误:
java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl cannot be cast to org.w3c.dom.Element
i try to get the element at the xml file, i want to check the element username and password, but there is an error about the cast class, it's seem difficult for me,... thanks before
我尝试在 xml 文件中获取元素,我想检查元素用户名和密码,但是关于 cast 类有错误,这对我来说似乎很困难,...谢谢之前
回答by Jon Skeet
This is the problem:
这就是问题:
Element pengguna = (Element) doc.getElementsByTagName("pengguna");
getElementsByTagName
doesn't return a single element - it returns multiple elements. You probably want something like:
getElementsByTagName
不返回单个元素 - 它返回多个元素。你可能想要这样的东西:
NodeList penggunas = doc.getElementsByTagName("pengguna");
if (penggunas.getLength() != 1) {
// Handle this - e.g. throw an exception
}
Element pengguna = (Element) penggunas.item(0);
EDIT: Later, you've got a bug here:
编辑:后来,你在这里有一个错误:
Node username = user.getElementsByTagName("username").item(i);
Node password = user.getElementsByTagName("password").item(i);
This should be:
这应该是:
Node username = user.getElementsByTagName("username").item(0);
Node password = user.getElementsByTagName("password").item(0);
You're already within the user
element - so you alwayswant the first username
and password
elements within that element. Otherwise you're asking for the second username
element within the second user
element, the third username
element within the third user
element etc. The numbering is relevant to the element that you're in, not some global count.
您已经在user
元素中 - 所以您总是想要该元素中的第一个username
和password
元素。否则,您将要求第二个username
元素中的第二个user
元素,第三个username
元素中的第三个user
元素等。编号与您所在的元素相关,而不是某些全局计数。
回答by eternay
getElementByTagName()
returns a NodeList
and you try to cast it to an Element
. This line is incorrect and will give you the ClassCastException
:
getElementByTagName()
返回 a ,NodeList
然后您尝试将其强制转换为Element
。这一行是不正确的,会给你ClassCastException
:
Element pengguna = (Element) doc.getElementsByTagName("pengguna");