首页 文章

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

提问于
浏览
4

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

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>

以下是我在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();

1 回答

  • 5

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

    String s = n.getNodeValue();
    

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


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

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

    编辑:代码_1143871是一个快速而又脏的 - 但重要的是,完整 - 程序(使用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;
        }
    }
    

相关问题