Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl 无法转换为 class javax.swing.text.Element

Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element

抛出这个程序我正在尝试解压缩我的 xml 文件并使用 SAX 解析它,对于解析部分我有一个异常出现关于 ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot转换为 class javax.swing.text.Element,这是我的代码:

import java.io.File;
import java.io.IOException;

import javax.swing.text.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Parser {
    public static void main(String[] args) throws IOException, ZipException {
        String source = "C:\Users\java_program\xml.zip";
        String destination = "C:\Users\java_program\xml";
        ZipFile zipFile = new ZipFile(source);
        zipFile.extractAll(destination);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File ("C:\Users\java_program\xml\xml.xml"));
            
            document.getDocumentElement().normalize();
            NodeList productList = document.getElementsByTagName("product");
            for(int i = 0; i<productList.getLength(); i++) {
                Node product = productList.item(i);
                if (product.getNodeType() == Node.ELEMENT_NODE) {
                    Element productElement = (Element) product; // the problem is here
                    NodeList productDetails = product.getChildNodes();
                    for(int j=0; j<productDetails.getLength(); j++) {
                        Node detail = productDetails.item(j);
                        if (product.getNodeType() == Node.ELEMENT_NODE) {
                            Element detailElement = (Element) detail; // the problem is here
                            System.out.println("      " + ((org.w3c.dom.Element) detailElement).getTagName() + ": " + detailElement.getAttributes());
                        }
                    }
                    
                }
                
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}

我的 XML 文件有这样的结构:


<?xml version="1.0" encoding="utf-8"?>
<import xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <products>
        <product>
            <article_sku>000c66dawt</article_sku>
            <brand>HUAWEI</brand>
            <brand_reference>HUAWEI-NEXUS-6P-64GO-ARGENT</brand_reference>
            <category>SMARTP</category>
            <ean13>6901443077359</ean13>
            <it_cpu_smartphone_type><![CDATA[Qualcomm Snapdragon S810]]></it_cpu_smartphone_type>
        </product>
        <product>
            <article_sku>000cfxlysl</article_sku>
            <brand>HERCULES</brand>
            <brand_reference>HERCULES-DJCONTROL-COMPACT</brand_reference>
            <category>PLATIN-1</category>
            <ean13>3362934745288</ean13>
        </product>
    </products>
</import>

非常感谢任何帮助

您输入错误 Element class。 变化

import javax.swing.text.Element;

import org.w3c.dom.Element;

您还创建了作为节点的 detail 对象,并检查之前创建的产品变量是否为元素,然后将 detail 转换为 Element。

Node detail = productDetails.item(j);
if (product.getNodeType() == Node.ELEMENT_NODE) {
    Element detailElement = (Element) detail; 

产品线:

  Node product = productList.item(i);

我认为你应该改成有条件的 detail.getNodetype() == Node.ELEMENT_NODE