1

I am trying to create an org.w3c.dom.Document object from an xml string. I have followed what many have suggested in other questions but the document ends up empty. What is wrong with the following code?

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

And the xml text in the string looks like the following (this comes from 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>

Can anyone see what is wrong with my code? I ultimately just want to run a couple of xpath queries on the document...

1
  • why do you say the document is "empty". also, what is response? Commented Sep 4, 2012 at 16:03

1 Answer 1

2

I would suggest to start with setting docFactory.setNamespaceAware(true);, otherwise the parsing, the DOM built and the XPath implementation will not be able to work with XML with namespaces as you have posted.

Sign up to request clarification or add additional context in comments.

1 Comment

I got the document to work by just taking the response stream and converting it straight to a document from the stream rather than converting it to string then to document. However, I upvoted and marked as answer because of the valuable piece of advice on setNameSpaceAware

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.