Solved

Access embeded encoded XML document in another XML document

Posted on 2004-10-22
195 Views
Last Modified: 2010-03-31
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.
0
Question by:trudyhlittle
    9 Comments
     
    LVL 86

    Accepted Solution

    by:
    >>but when I create my DOM object, the XML document is just a text value of my element

    Can you show how you're doing that?
    0
     

    Author Comment

    by:trudyhlittle
    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 )));
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Hang on - just looking again - that was wrong

    why dont you just do this:

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

    ?
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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 ;-)
    0
     

    Author Comment

    by:trudyhlittle
    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.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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

    0
     

    Author Comment

    by:trudyhlittle
    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.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    No problem ;-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now