0

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

            }
        }
4

1 に答える 1

2

org.w3c.dom.Document を jdom.xpath.XPath.selectSingleNode() に渡していますが、そのメソッドは org.jdom.Document または org.jdom.Element を想定しています。

XML を JDOM ドキュメントとして解析し、Jaxen を使用して XPath クエリを実行する 1 つの方法を次に示します。これもクラスパスに含まれている必要があります。

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 に答える