0

Im trying to parse the input stream from a zipfile zip entry and trying to create a org.w3c.dom.Document but for some reason im getting a DefferedDocumentImpl back. Im also creating a new org.w3c.dom.Document and this is returning a DocumentImpl. Then using Xpath to select a single node but im getting this error "org.apache.xerces.dom.DocumentImpl incompatible with org.jdom.Element" when Im trying to find my specific node. Ive done some searching but cant seem to find and examples. Anyone know why im not getting my docs created as dom docs? Thanks in advance for the help.

            //create a zip file from the crate location
        File downloadFile = crate.getLocation();
        ZipFile zipFile = new ZipFile(downloadFile);

        //put all the contents of the zip file into an enumeration
        Enumeration entries = zipFile.entries();

        while (entries.hasMoreElements()){
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String currentEntry = entry.getName();

            if (currentEntry.equals("ATTACH 8130-3 XML/signature.xml")){
                InputStream zipStream = zipFile.getInputStream(entry);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                org.w3c.dom.Document doc = (org.w3c.dom.Document)dBuilder.parse(zipStream);
                doc.getDocumentElement().normalize();
                NodeList certNode = doc.getElementsByTagName("ATA_PartCertificationForm");
                int testInt = certNode.getLength();
                org.w3c.dom.Document doc2 = (org.w3c.dom.Document) dBuilder.newDocument();
                Node parentNode = doc.getParentNode();
                Element rootElement = doc2.createElement("CurrentCertificate");
                doc2.appendChild(rootElement);
                for(int i=0; i<certNode.getLength(); i++){
                    Node childNode = certNode.item(i);
                    Element childElement;
                    childElement = (Element)certNode.item(i);
                    rootElement.appendChild(doc2.importNode(childNode, true));
                    String nameString = childNode.getNodeName();
                    Element block13Element = (Element) XPath.selectSingleNode(doc2, "//Block13M");
                    System.out.println("tester test");
                }
                System.out.println("Test break");

            }
        }
Kevin Lillybridge
  • 289
  • 1
  • 4
  • 8
  • I assume you're using JDOM + Jaxen? – dnault May 15 '12 at 22:11
  • No I'm not using Jaxen. I was trying to use jdom.Xpath. What Im really trying to do is extract an xml file from a zipfile. Then create new XML using some Elements from the extracted xml file. But getting compatibility errors. – Kevin Lillybridge May 15 '12 at 22:27
  • It's my understanding that JDOM uses Jaxen as its default XPath engine. Once you get past your current exception, you may encounter java.lang.NoClassDefFoundError: org/jaxen/jdom/JDOMXPath -- which you can resolve by adding jaxen to your classpath. – dnault May 15 '12 at 22:36

1 Answers1

2

You're passing an org.w3c.dom.Document to jdom.xpath.XPath.selectSingleNode(), but that method expects an org.jdom.Document or an org.jdom.Element.

Here's one way to parse your XML as a JDOM document and execute an XPath query using Jaxen, which must also be in the classpath.

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class JdomXpathSandbox {
    public static void main(String[] args) throws Exception {
        InputStream is = ...;
        Document document = new SAXBuilder().build(is);
        Element rootElement = document.getRootElement();

        String xpathExpression = ...
        List matchingNodes = XPath.selectNodes(rootElement, xpathExpression);
    }
}
dnault
  • 8,340
  • 1
  • 34
  • 53
  • Ah, I see. i just changed everything to use a the JDOM doc and SAXBuilder and it works fine. Rookie mistake. Thanks for the help! – Kevin Lillybridge May 16 '12 at 17:29
  • You are welcome! If you'd like to mark this as the accepted answer I would be similarly grateful ;) – dnault May 17 '12 at 01:49