gpt4 book ai didi

java - 为什么我在尝试创建 JDOM 文档时得到 DefferedDocumentImpl?

转载 作者:行者123 更新时间:2023-12-02 07:47:38 26 4
gpt4 key购买 nike

我正在尝试从 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");

}
}

最佳答案

您正在将 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);
}
}

关于java - 为什么我在尝试创建 JDOM 文档时得到 DefferedDocumentImpl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com