如何解决Java XML:ClassCastException 类 com.sun.org.apache.xerces.internal.dom.DeferredTextImpl 不能转换为类 javax.swing.text.Element
抛出这个程序我正在尝试解压缩我的 xml 文件并使用 SAX 解析它,对于解析部分,我有一个关于 ClassCastException 类出现的异常 com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot被转换为类 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
类。
改变
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。