Throw this program I'm trying to unzip my xml file and parse it using SAX, for the parsing part I have an exception which appeared about ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element, here is my code :
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();
}
}
}
My XML file have this structure :
<?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>
Any help is really appreciated
You imported the wrong Element
class.
Change
import javax.swing.text.Element;
to
import org.w3c.dom.Element;
Also you create detail object which is a node and check that if the previously created product variable is an element and then cast detail to Element.
Node detail = productDetails.item(j);
if (product.getNodeType() == Node.ELEMENT_NODE) {
Element detailElement = (Element) detail;
the product line:
Node product = productList.item(i);
I think you should chacnge to conditional to detail.getNodetype() == Node.ELEMENT_NODE