在java中从xml字符串创建org.w3c.dom.Document时遇到问题
我正在尝试从xml字符串创建org.w3c.dom.Document对象。我遵循了许多人在其他问题中提出的建议,但文件最终是空的。以下代码有什么问题在java中从xml字符串创建org.w3c.dom.Document时遇到问题,java,xml,Java,Xml,我正在尝试从xml字符串创建org.w3c.dom.Document对象。我遵循了许多人在其他问题中提出的建议,但文件最终是空的。以下代码有什么问题 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new Inp
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(response.getResponseText())));
字符串中的xml文本如下所示(来自response.getResponseText())
http://www.blah.com/ns/2006/05/01/webservices/123/TokenManagement_1/CreateServiceToken_1_Reply
2012-09-04T15:35:06.8116593Z
ba04425d-d93e-4a70-a134-ab8e29d5345c}
2012-09-04T17:04:19.1834228Z
3DEC2723A01047D1590544CBA5BA5BA1E30326535E609DC1E6FAC5C659BC3B8A693BB054834A58B25037ED830CD05784DB176A62309EB4B608C6F0B5B3F13DE0EC56BE9F822ACFA3B549D4427D89BF030BF48BA671DCAEB4940EFEB71 FB71
有人能看到我的代码有什么问题吗?我最终只想在文档上运行几个xpath查询…我建议从设置
docFactory.setNamespaceAware(true)开始代码>,否则解析、构建的DOM和XPath实现将无法使用您发布的名称空间的XML。为什么说文档是“空的”。还有,什么是response
?我只需要获取响应流并将其直接从流转换为文档,而不是将其转换为字符串然后再转换为文档,就可以让文档正常工作。然而,由于setNameSpaceAware上有一条宝贵的建议,我投票并将其标记为答案
<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>