1

我正在尝试从 xml 字符串创建一个 org.w3c.dom.Document 对象。我遵循了许多人在其他问题中的建议,但文件最终是空的。以下代码有什么问题?

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(response.getResponseText())));

字符串中的 xml 文本如下所示(来自 response.getResponseText())

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
    <a:Action s:mustUnderstand="1">http://www.blah.com/ns/2006/05/01/webservices/123/TokenManagement_1/CreateServiceToken_1_Reply</a:Action>
    <CacheResponse xsi:type="DoNotStoreCacheResponse" xmlns="http://www.blah.com/ns/2008/03/01/webservices/123/Cache_1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Date>2012-09-04T15:35:06.8116593Z</Date>
        <DoNotStore />
    </CacheResponse>
    <a:RelatesTo>ba04425d-d93e-4a70-a134-ab8e29d5345c}</a:RelatesTo>
</s:Header>
<s:Body>
    <CreateServiceToken_Response_1 xmlns="http://www.blah.com/ns/2006/05/01/webservices/123/TokenManagement_1" xmlns:global="http://www.blah.com/ns/2006/05/01/webservices/123/Common_1">
        <Expiration>2012-09-04T17:04:19.1834228Z</Expiration>
        <global:Token>3DEC2723A01047D1590544CBA5BA1E30326535E609DC1E6FAC5C659BC3B8A693BB054834A58B235037ED830CD05784DB176A62309AEB4B608C6F0B5B3F13ADE0EC56BE9F822ACFA3B549D4427D89BF030BFF48BA671DCAEB49940EFEBDEBFB71</global:Token>
    </CreateServiceToken_Response_1>
</s:Body>

谁能看到我的代码有什么问题?我最终只想在文档上运行几个 xpath 查询......

4

1 回答 1

2

我建议从设置开始docFactory.setNamespaceAware(true);,否则解析、构建的 DOM 和 XPath 实现将无法使用您发布的带有名称空间的 XML。

于 2012-09-04T16:04:45.857 回答