为什么在尝试创建JDOM文档时会收到DefferedDocumentImpl?

时间:2012-05-15 21:39:42

标签: java

我试图从zipfile zip条目解析输入流并尝试创建org.w3c.dom.Document但由于某种原因我得到了DefferedDocumentImpl。我还创建了一个新的org.w3c.dom.Document,这将返回一个DocumentImpl。然后使用Xpath选择单个节点,但是当我试图找到我的特定节点时,我得到这个错误“org.apache.xerces.dom.DocumentImpl与org.jdom.Element不兼容”。我做了一些搜索,但似乎无法找到和例子。任何人都知道为什么我没有将我的文档创建为dom文档?在此先感谢您的帮助。

            //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");

            }
        }

1 个答案:

答案 0 :(得分:2)

您正在将org.w3c.dom.Document传递给jdom.xpath.XPath.selectSingleNode(),但该方法需要org.jdom.Document或org.jdom.Element。

这是将XML解析为JDOM文档并使用Jaxen执行XPath查询的一种方法,Jaxen也必须位于类路径中。

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);
    }
}