Link to home
Start Free TrialLog in
Avatar of trudyhlittle
trudyhlittleFlag for United States of America

asked on

Access embeded encoded XML document in another XML document

I have an XML document (SOAP message) that contains an XML document that is encoded.  I need to be able to access the information in the embeded document as I would any other XML document.

Example I have the following document.  The PostMessageResult element contains an encoded XML document.  I want to be able to access each element of embeded document as part of the larger document but when I create my DOM object, the XML document is just a text value of my element.  How do I decode the embeded XML document so that when I create my DOM object the XML document is included as part of the XML document not as text but as nodes?

I have this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
      <PostMessageResponse xmlns="https://secure.CompanyName.com">      <PostMessageResult>&lt;CompanyName&gt;&lt;StatusCode&gt;0&lt;/StatusCode&gt;&lt;StatusMessage&gt;Success&lt;/StatusMessage&gt;&lt;ResponseXML&gt;&lt;CompanyName                   CompanyNameID="3241553" SourceID="1000" DestinationID="1077" TransDate="10/21/2004 8:06:26 AM"&gt;&lt;Transaction TransactionID="4052836"&gt;&lt;Message&gt;                  &lt;MessageType&gt;Message_Confirmation&lt;/MessageType&gt;&lt;SourceBusinessRef&gt;63650&lt;/SourceBusinessRef&gt;&lt;SourceRoutingRef&gt;&lt;/SourceRoutingRef&gt;                  &lt;SourceLoanNumber&gt;&lt;/SourceLoanNumber&gt;&lt;DestinationBusinessRef&gt;04-1717 &lt;/DestinationBusinessRef&gt;&lt;DestinationRoutingRef&gt;      &lt;/DestinationRoutingRef&gt;            &lt;DestinationLoanNumber&gt;&lt;/DestinationLoanNumber&gt;&lt;AdditionalInfo&gt;Message entered into CMS successfully.&lt;/AdditionalInfo&gt;      &lt;/Message&gt;&lt;/Transaction&gt;            &lt;/CompanyName&gt;&lt;/ResponseXML&gt;&lt;/CompanyName&gt;
                  </PostMessageResult>
            </PostMessageResponse>
      </soap:Body>
</soap:Envelope>

I want this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">      <soap:Body>
            <PostMessageResponse xmlns="https://secure.CompanyName.com">
                  <PostMessageResult>
                        <CompanyName>
                              <StatusCode>0</StatusCode>
                              <StatusMessage>Success</StatusMessage>
                              <ResponseXML>
                                    <CompanyName CompanyNameID="3241553" SourceID="1000" DestinationID="1077" TransDate="10/21/2004 8:06:26 AM">
                                          <Transaction TransactionID="4052836">
                                                <Message>
                                                      <MessageType>Message_Confirmation</MessageType>
                                                      <SourceBusinessRef>63650</SourceBusinessRef>
                                                      <SourceRoutingRef></SourceRoutingRef>
                                                      <SourceLoanNumber></SourceLoanNumber>
                                                      <DestinationBusinessRef>04-1717</DestinationBusinessRef>
                                                      <DestinationRoutingRef></DestinationRoutingRef>
                                                      <DestinationLoanNumber></DestinationLoanNumber>
                                                      <AdditionalInfo>Message entered into CMS successfully.</AdditionalInfo>
                                                </Message>
                                          </Transaction>
                                    </CompanyName>
                              </ResponseXML>
                        </CompanyName>
                  </PostMessageResult>
            </PostMessageResponse>
      </soap:Body>
</soap:Envelope>


Thanks.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trudyhlittle

ASKER

Sorry for the delay.  Busy weekend.

Here is the code I'm using to create the Document object.  buffer is a variable that holds a string retrieved from the database that looks like the first example above (under the "I have this:" text).

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
builder = factory.newDocumentBuilder();
Document result = builder.parse( new InputSource( new StringReader( buffer )));
What you need to do is to do that again. You should build an InputSource the second time from the
node that represents the embedded document
Hang on - just looking again - that was wrong

why dont you just do this:

whatYouHave = whatYouHave.replaceAll("&lt;", "<");
whatYouHave = whatYouHave.replaceAll("&gt;", ">");

?
Hang on again - think i was right both times ;-)

You need to apply my second comment first (to the node as String) and my first second ;-)
Thanks for the input.  I actually only needed to do your first suggestion.  Creating another document using the embeded xml automatically decoded the document.

I think I might have been able to do your second suggestion on the whole document and then create only one document, but the first one worked, so I'll quit while I'm ahead.  :o)

Thanks.
8-)

>>Creating another document using the embeded xml automatically decoded the document.

Yes i thought that might happen.

btw, it doesn't matter hugely, but for future reference, it's better to try to mark the comment that's closest to the
actual answer as the 'accepted' one for future viewers

I know, sorry.  I meant to select your first suggestion but I grabbed the first response instead and obviously didn't take the time to read the screen before I rated it.  My bad.  I'll be more careful next time.
No problem ;-)