. lang。不能对DeepNodeListImpl进行cast

时间:2023-01-21 00:18:55

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类有一个错误,这对我来说很难,……由于之前

2 个解决方案

#1


4  

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:

编辑:稍后,您有一个bug:

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 always want 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元素中了——所以您总是希望在该元素中包含第一个用户名和密码元素。否则,您将在第二个用户元素中请求第二个用户名元素,第三个用户元素中的第三个用户名元素等等。编号与您所在的元素相关,而不是一些全局计数。

#2


1  

getElementByTagName() returns a NodeList and you try to cast it to an Element. This line is incorrect and will give you the ClassCastException:

getElementByTagName()返回一个节点列表,并尝试将其转换为元素。这行不正确,会给你ClassCastException:

Element pengguna = (Element) doc.getElementsByTagName("pengguna");

#1


4  

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:

编辑:稍后,您有一个bug:

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 always want 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元素中了——所以您总是希望在该元素中包含第一个用户名和密码元素。否则,您将在第二个用户元素中请求第二个用户名元素,第三个用户元素中的第三个用户名元素等等。编号与您所在的元素相关,而不是一些全局计数。

#2


1  

getElementByTagName() returns a NodeList and you try to cast it to an Element. This line is incorrect and will give you the ClassCastException:

getElementByTagName()返回一个节点列表,并尝试将其转换为元素。这行不正确,会给你ClassCastException:

Element pengguna = (Element) doc.getElementsByTagName("pengguna");