1

I am trying to parse through an XML which looks something like This is a xml response of an image which i need to render in my page,can somebody help me out to parse this through XmlStreamReader .

<?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
 <soapenv:Header>
   <wsa:MessageID soapenv:mustUnderstand="0"> uuid:c39fe700-7ebe-11e4-bcd2-833631db0ec0</wsa:MessageID>
   <wsa:To soapenv:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
 </soapenv:Header>

<soapenv:Body>
   <GetObjectResponse xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
   <GetObjectResponse xsi:type="ns1:GetObjectResult" xmlns:ns1="http://s3.amazonaws.com/doc/2006-03-01/">
 <Status>
    <Code>200</Code>
    <Description>OK</Description>
 </Status>
 <Metadata>
    <Name>Content-Type</Name>
    <Value>image/jpeg</Value>
 </Metadata>
<Data>......
.
.
.
This is a reponse of an Image....... 
.
.
.
</Data>
    <LastModified>2014-11-14T06:18:18.000Z
    </LastModified>
   <ETag>&quot;e9bd9987d269f6b638a1f9a0deb3f911&quot;</ETag>
  </GetObjectResponse>
</GetObjectResponse>
</soapenv:Body>
</soapenv:Envelope>
2
  • DOM Document is easier to use. Do you have a reason that you want to use XmlStreamReader instead?
    – Keith C
    Commented Dec 8, 2014 at 11:26
  • Not specific . I want to parse this response to get to it . If you have any idea about DOM Document , could you please share it . Stucked and want the help
    – JJoseph
    Commented Dec 8, 2014 at 12:16

1 Answer 1

2

With no error checking (such as checking the nesting of the elements and checking the status code) something like this should work:

String envNs = 'http://schemas.xmlsoap.org/soap/envelope/';
String docNs = 'http://s3.amazonaws.com/doc/2006-03-01';

String xml = ...;
Dom.Document doc = new Dom.Document();
doc.load(xml);
String data = doc
        .getRootElement()
        .getChildElement('Body', envNs)
        .getChildElement('GetObjectResponse', docNs)
        .getChildElement('GetObjectResponse', docNs)
        .getChildElement('Data', docNs)
        .getText();

where the main method used is getChildElement.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .