builder.parse((new StringReader(xml))返回DeferredDocumentImpl

use*_*381 5 java

我试图理解我可能犯的错误但无法找到解决方案.

public static Document getXMLFromString(String xml) {
        org.w3c.dom.Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder;
            builder = factory.newDocumentBuilder();
            doc = (org.w3c.dom.Document) builder.parse(new InputSource(
                    new StringReader(xml)));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return doc;
    }

我做了导入org.w3c.dom.Document

我在这里称这个方法:

private Node getAuthToken(SOAPMessage responseAuth) throws SOAPException,
            TransformerException, ParserConfigurationException, IOException,
            SAXException {
        String s = indentXML(responseAuth.getSOAPPart().getContent());
        Document doc = getXMLFromString(s);
        NodeList authTokenNodeList = doc.getElementsByTagName("authToken");
        return authTokenNodeList.item(0);
    }

NodeList为空.

在网上研究之后,每个人都使用此代码将字符串解析为Document.我没有任何异常但是在调用方法parse()之后,doc的值被设置为[#document:null] DeferredDocumentImpl.

我正在使用org.w3c.dom中的所有内容.

xml是包含的字符串

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<context xmlns="urn:zimbra">
<session id="36" type="admin">36</session>
<change token="251"/>
</context>
</soap:Header>
<soap:Body>
<AuthResponse xmlns="urn:zimbraAdmin">
<authToken>...</authToken>
<lifetime>...</lifetime>
<a n="zimbraIsDomainAdminAccount">false</a>
<session id="36" type="admin">36</session>
</AuthResponse>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

以下是我在SOAP调用后构建字符串的方法:

String xml = indentXML(responseAuth.getSOAPPart().getContent());

我究竟做错了什么?

这就是我想以一种简单的方式做的事情:

StringBuilder soapResponse = new StringBuilder(...
...
...
);

        org.w3c.dom.Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder;
            builder = factory.newDocumentBuilder();
            doc = (org.w3c.dom.Document) builder.parse(new InputSource(
                    new StringReader(soapResponse.toString())));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        NodeList authTokenNodeList = doc.getElementsByTagName("authToken");
        Node n = authTokenNodeList.item(0);
        String s = n.getNodeValue();

Jon*_*eet 6

编辑:看看你的更新代码,我认为这是问题:

String s = n.getNodeValue();
Run Code Online (Sandbox Code Playgroud)

如果你查看Node的文档,你会看到它getNodeValue()被定义为返回null元素...因此问题.我的示例代码使用getTextContent()相反,它工作正常.


看起来这只是推迟扩展内存中的对象,直到它们被要求为止.

您是否尝试在返回的文档上调用方法而不是仅仅查看调试器?我怀疑一切都按预期工作.如果没有,请发布一个简短但完整的程序,表明它行为不端.(请注意,在您给出的样本中,您甚至没有显示如何builder设置,并且您还没有使用过factory.)

编辑:你给的代码对我有用.这是一个快速且肮脏但非常重要的完整程序(使用Guava将XML文件加载到字符串中),它显示了成功找到的节点:

import org.w3c.dom.*;
import org.xml.sax.*;
import java.text.*;
import java.util.*;
import javax.xml.parsers.*;
import java.io.*;
import com.google.common.base.*;
import com.google.common.io.*;

public class Test {

    public static void main(String[] args) throws Exception {
        String xml = Files.toString(new File("test.xml"), Charsets.UTF_8);
        Node node = getAuthToken(xml);
        System.out.println(node.getTextContent());
    }

    private static Node getAuthToken(String xml) throws Exception {
        Document doc = getXMLFromString(xml);
        NodeList authTokenNodeList = doc.getElementsByTagName("authToken");
        return authTokenNodeList.item(0);
    }

    public static Document getXMLFromString(String xml) throws Exception {
        Document doc = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory
            .newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
        return doc;
    }
}
Run Code Online (Sandbox Code Playgroud)