0

I connect to Webex and retrieve the timezone list (XML with namespaces) and try to parse response via res.getBodyDocument() or doc.load but I only get document[]. I have read a lot of other similar questions but the answers do not work for the XML response I get.

Apex code:

Http http           = new Http();
HttpRequest req   = new HttpRequest(); 
HttpResponse res    = new HttpResponse();

string EndPoint = 'https://testco.webex.com/WBXService/XMLService'; // Endpoint depending on site
req.setEndpoint( EndPoint );
req.setMethod('GET');      
req.setBody(requestBodyXML);            
req.setHeader('Content-Type', 'application/xml');

res = http.send(req);
string xmlResponse = res.getBody();
system.debug('xml: '+ xmlResponse);
Dom.Document doc = res.getBodyDocument();
/*Dom.Document domDoc = new Dom.Document();
domDoc.load(xmlResponse); */
system.debug('doc: '+ domDoc);
Dom.XMLNode timeZone = domDoc.getRootElement();
system.debug('tz: '+ timeZone.getName());

Sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:ns1="http://www.webex.com/schemas/2002/06/service/site" xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
   <serv:header>
      <serv:response>
         <serv:result>SUCCESS</serv:result>
         <serv:gsbStatus>PRIMARY</serv:gsbStatus>
      </serv:response>
   </serv:header>
   <serv:body>
      <serv:bodyContent xsi:type="ns1:lstTimeZoneResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <ns1:timeZone>
            <ns1:timeZoneID>0</ns1:timeZoneID>
            <ns1:gmtOffset>720</ns1:gmtOffset>
            <ns1:description>GMT+12:00, Dateline (Marshall Islands)</ns1:description>
            <ns1:shortName>Marshall Islands</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>1</ns1:timeZoneID>
            <ns1:gmtOffset>-660</ns1:gmtOffset>
            <ns1:description>GMT-11:00, Samoa (Samoa)</ns1:description>
            <ns1:shortName>Samoa</ns1:shortName>
            <ns1:hideTimeZoneName>true</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>2</ns1:timeZoneID>
            <ns1:gmtOffset>-600</ns1:gmtOffset>
            <ns1:description>GMT-10:00, Hawaii (Honolulu)</ns1:description>
            <ns1:shortName>Honolulu</ns1:shortName>
            <ns1:hideTimeZoneName>true</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>3</ns1:timeZoneID>
            <ns1:gmtOffset>-540</ns1:gmtOffset>
            <ns1:description>GMT-09:00, Alaska (Anchorage)</ns1:description>
            <ns1:shortName>Anchorage</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
            <ns1:standardLabel>Standard</ns1:standardLabel>
            <ns1:daylightLabel>Daylight</ns1:daylightLabel>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>4</ns1:timeZoneID>
            <ns1:gmtOffset>-480</ns1:gmtOffset>
            <ns1:description>GMT-08:00, Pacific (San Francisco)</ns1:description>
            <ns1:shortName>San Francisco</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
            <ns1:standardLabel>Standard</ns1:standardLabel>
            <ns1:daylightLabel>Daylight</ns1:daylightLabel>
         </ns1:timeZone>
         <ns1:timeZone>
            ...
            <ns1:timeZoneID>156</ns1:timeZoneID>
            <ns1:gmtOffset>420</ns1:gmtOffset>
            <ns1:description>GMT+07:00, Novosibirsk (Novosibirsk)</ns1:description>
            <ns1:shortName>Novosibirsk</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>157</ns1:timeZoneID>
            <ns1:gmtOffset>120</ns1:gmtOffset>
            <ns1:description>GMT+02:00, Romania (Bucharest)</ns1:description>
            <ns1:shortName>Bucharest</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
            <ns1:daylightLabel>Daylight</ns1:daylightLabel>
         </ns1:timeZone>
         <ns1:timeZone>
            <ns1:timeZoneID>158</ns1:timeZoneID>
            <ns1:gmtOffset>-300</ns1:gmtOffset>
            <ns1:description>GMT-05:00, Eastern (Toronto)</ns1:description>
            <ns1:shortName>Toronto</ns1:shortName>
            <ns1:hideTimeZoneName>false</ns1:hideTimeZoneName>
            <ns1:fallInDST>false</ns1:fallInDST>
            <ns1:standardLabel>Standard</ns1:standardLabel>
            <ns1:daylightLabel>Daylight</ns1:daylightLabel>
         </ns1:timeZone>
      </serv:bodyContent>
   </serv:body>
</serv:message>
2
  • 1
    Your naming is quite misleading. If you receive an XML payload, don't call the variable jsonResponse.
    – Adrian Larson
    Commented Nov 14, 2017 at 17:47
  • Thanks, yes makes sense to have a better variable name.
    – Arad
    Commented Nov 14, 2017 at 17:52

1 Answer 1

1

The debug output for the DOM namespace is not super useful and it doesn't mean anything that your output reads Document[]. Don't depend on debugging behavior to navigate the structure for you. Instead use XmlNode methods, specifically getChildElement.

String response = '<?xml version="1.0" encoding="UTF-8"?>...';
Dom.Document document = new Dom.Document();
document.load(response);

String serv = 'http://www.webex.com/schemas/2002/06/service',
    ns1='http://www.webex.com/schemas/2002/06/service/site';
Dom.XmlNode root = document.getRootElement(),
    body = root.getChildElement('body', serv),
    content = body.getChildElement('bodyContent', serv);
for (Dom.XmlNode node : content.getChildElements())
{
    String timezoneId = node.getChildElement('timeZoneID', ns1).getText();
    // etc
}
3
  • Thank you Adrian for your solution. I hardcoded a small sample response with only one timezone and it worked well. My problem though is that I have in all some 160 timezones and I cannot hardcode them in first line of code as they can change based on daylight savings etc: String response = '<?xml version="1.0" encoding="UTF-8"?>...'; How can I use the xmlresponse from http to webex and load it into the document that I can then parse? Thanks in advance.
    – Arad
    Commented Nov 14, 2017 at 22:51
  • You just do document.load(response.getBody())...
    – Adrian Larson
    Commented Nov 15, 2017 at 0:35
  • It is all working as expected now. Thank you so much for your help:)
    – Arad
    Commented Nov 15, 2017 at 2:09

You must log in to answer this question.

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