0

我试图从 zipfile zip 条目解析输入流并尝试创建 org.w3c.dom.Document 但由于某种原因我得到了 DeferedDocumentImpl 回来。我还创建了一个新的 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");

            }
        }
4

1 回答 1

2

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

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

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);
    }
}
于 2012-05-15T22:29:54.980 回答