4

This is the monster I receive after a GET request

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
    <param>
        <value>
            <array>
                <data>
                    <value>
                        <struct>
                            <member>
                                <name>DateCreated</name>
                                <value><dateTime.iso8601>20160830T12:57:13</dateTime.iso8601></value>
                            </member>
                            <member>
                                <name>Id</name>
                                <value><i4>17</i4></value>
                            </member>
                        </struct>
                    </value>
                    <value>
                        <struct>
                            <member>
                                <name>DateCreated</name>
                                <value><dateTime.iso8601>20160830T15:57:25</dateTime.iso8601></value>
                            </member>
                            <member>
                                <name>Id</name>
                                <value><i4>43</i4></value>
                            </member>
                        </struct>
                    </value>
                </data>
            </array>
        </value>
    </param>
</params>
</methodResponse>

I want to get the DateCreated and Id values.

The server may send multiple Ids with different created dates. Is it possible to compare the DateCreated values to get the most recent value?

Here is what I've been able to come with after looking at the docs

Dom.Document doc = res.getBodyDocument();

Dom.XmlNode methodResponse = doc.getRootElement();

String dateCreated = methodResponse.getChildElement('params', null)
                    .getChildElement('value', null)
                    .getChildElement('array', null)
                    .getChildElement('data', null)
                    .getChildElement('value', null)
                    .getChildElement('struct', null)
                    .getChildElement('member', null)
                    .getChildElement('value', null).getText();

String theId = methodResponse.getChildElement('params', null)
                    .getChildElement('value', null)
                    .getChildElement('array', null)
                    .getChildElement('data', null)
                    .getChildElement('value', null)
                    .getChildElement('struct', null)
                    .getChildElement('member', null)
                    .getChildElement('value', null).getText();
return theId;

The return value from theId is

Text Node Value [154]|methodResponse|"XMLNode[ELEMENT,methodResponse,null,null,null,[XMLNode[ELEMENT,params,null,null,null,[XMLNode[ELEMENT,param,null,null,null,[XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,array,null,null,null,[XMLNode[ELEMENT,data,null,null,null,[XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,struct,null,null,null,[XMLNode[ELEMENT,member,null,null,null,[XMLNode[ELEMENT,name,null,null,null,[XMLNode[TEXT,null,null,null,null,null,DateCreated,]],null,], XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,dateTime.iso8601,null,null,null,[XMLNode[TEXT,null,null,null,null,null,20160830T12:57:13,]],null,]],null,]],null,], XMLNode[ELEMENT,member,null,null,null,[XMLNode[ELEMENT,name,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Id,]],null,], XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,i4,null,null,null,[XMLNode[TEXT,null,null,null,null,null,17,]],null,]],null,]],null,]],null,]],null,], XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,struct,null,null,null,[XMLNode[ELEMENT,member,null,null,null,[XMLNode[ELEMENT,name,null,null,null,[XMLNode[TEXT,null,null,null,null,null,DateCreated,]],null,], XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,dateTime.iso8601,null,null,null,[XMLNode[TEXT,null,null,null,null,null,20160830T15:57:25,]],null,]],null,]],null,], XMLNode[ELEMENT,member,null,null,null,[XMLNode[ELEMENT,name,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Id,]],null,], XMLNode[ELEMENT,value,null,null,null,[XMLNode[ELEMENT,i4,null,null,null,[XMLNode[TEXT,null,null,null,null,null,43,]],null,]],null,]],null,]],null,]],null,]],null,]],null,]],null,]],null,]],null,]],null,]"|0x7d6901eb

  • 1
    Take a look at Reading and Writing XML Using the DOM. You should be able to figure out how to parse a List<Datetime> and just sort it. – Adrian Larson Aug 30 '16 at 22:26
  • 1
    Thanks for your question. I'm going to add an XmlRpc class to my upcoming library now. – sfdcfox Aug 30 '16 at 22:30
  • 1
    @AdrianLarson eXtensible Markup Language-Remote Procedure Call. It's a standard protocol for calling a method on a server, like SOAP, but more bloated. – sfdcfox Aug 30 '16 at 22:35
  • @AdrianLarson if you could look at my updated post after looking at the link you shared I'd appreciate it! :D – Tyler Zika Aug 30 '16 at 22:51
  • 1
    I applaud your use of chaining, but to debug this error you may want to abandon it temporarily. Try breaking out each node separately and debugging them to see which one is null. I'm guessing the . in the dateTime.iso8601 is giving you problems, but I haven't the time to check. – Adrian Larson Aug 30 '16 at 22:52
2

The below function will return the most recent Id from your HttpResponse.

public Id getLatestId(HttpResponse res){
    Dom.Document doc = res.getBodyDocument();
    Dom.XmlNode methodResponse = doc.getRootElement();

    List<Dom.XmlNode> dataNodes = methodResponse.getChildElement('params', null)
                    .getChildElement('param', null)
                    .getChildElement('value', null)
                    .getChildElement('array', null)
                    .getChildElement('data', null)
                    .getChildElements();

    List<DateTime> datesToSort = new List<DateTime>();
    Map<DateTime, String> dateToIdMap = new Map<DateTime, String>();

    for(Dom.XmlNode dNode : dataNodes){
        List<Dom.XmlNode> memberNodes = dNode.getChildElement('struct', null)
                    .getChildElements();

        DateTime createdDate = null;
        String id = '';

        for(Dom.XmlNode mNode : memberNodes){
            String name = mNode.getChildElement('name', null)
                    .getText();

            if(name == 'DateCreated'){
                String dt  = mNode.getChildElement('value', null)
                    .getChildElement('dateTime.iso8601', null)
                    .getText();
                //need to prepare the DateTime string for JSON parsing
                dt = dt.substring(0, 4) + '-' + dt.substring(4,6) + '-' + dt.substring(6, dt.length());
                createdDate = (DateTime) JSON.deserialize('"'+dt+'"', DateTime.class);
            }else if(name == 'Id'){
                id  = mNode.getChildElement('value', null)
                    .getChildElement('i4', null)
                    .getText();
            }     
        }

        datesToSort.add(createdDate);
        dateToIdMap.put(createdDate, id);  
    }

    datesToSort.sort();
    String latestId = dateToIdMap.get(datesToSort.get(datesToSort.size()-1));
    System.debug(latestId);
    return latestId;
}
0

Try this:

String s = '<?xml version="1.0" encoding="UTF-8"?>' +
'<methodResponse>'+
'<params>'+
'    <param>'+
'        <value>'+
'            <array>'+
'                <data>'+
'                    <value>'+
'                        <struct>'+
'                            <member>'+
'                                <name>DateCreated</name>'+
'                                <value><dateTime.iso8601>20160830T12:57:13</dateTime.iso8601></value>'+
'                            </member>'+
'                            <member>'+
'                                <name>Id</name>'+
'                                <value><i4>17</i4></value>'+
'                            </member>'+
'                        </struct>'+
'                    </value>'+
'                    <value>'+
'                        <struct>'+
'                            <member>'+
'                                <name>DateCreated</name>'+
'                                <value><dateTime.iso8601>20160830T15:57:25</dateTime.iso8601></value>'+
'                            </member>'+
'                            <member>'+
'                                <name>Id</name>'+
'                                <value><i4>43</i4></value>'+
'                            </member>'+
'                        </struct>'+
'                    </value>'+
'                </data>'+
'            </array>'+
'        </value>'+
'    </param>'+
'</params>'+
'</methodResponse>';


Dom.Document doc = new Dom.Document();
doc.load(s);
Dom.XmlNode methodResponse = doc.getRootElement();

String dateCreated = methodResponse.getChildElement('params', null)
                    .getChildElement('param', null)
                    .getChildElement('value', null)
                    .getChildElement('array', null)
                    .getChildElement('data', null)
                    .getChildElement('value', null)
                    .getChildElement('struct', null)
                    .getChildElement('member', null)
                    .getChildElement('value', null)
                    .getChildElement('dateTime.iso8601', null)
                    .getText();
System.debug('>>' + dateCreated);

String theId = methodResponse.getChildElement('params', null)
                    .getChildElement('param', null)
                    .getChildElement('value', null)
                    .getChildElement('array', null)
                    .getChildElement('data', null)
                    .getChildElement('value', null)
                    .getChildElement('struct', null)
                    .getChildElements()[1]
                    .getChildElement('value', null)
                    .getChildElement('i4', null).getText();

System.debug('>>' + theId );

Once you get the ID and date then you can easily find most recent value.

  • I'm trying to convert my XML string from a HttpResponse to a string. Right now it's HttpResponse res = http.send(req); Then I do res.getBody() to get the XML string. – Tyler Zika Aug 31 '16 at 17:35
  • are you facing any issue while covering? please clearly tell us what do want. – AtulRajguru9 Sep 1 '16 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.