2

I am doing a web service call and get XML response back. I need to extract the data within the XML node and create a map out of it. The issue is not the XML but the way the data is present in the node. Below is the response I get:

<RETS ReplyCode="0" ReplyText="Operation successful">
<RETS-RESPONSE>
MemberName = Member 22
User = 42870,0,Third Party,42870
Broker = 3637
MetadataVersion = 01.01.00057
MetadataTimestamp = Mon, 24 Oct 2011 12:16:01 GMT
MinMetadataVersion = 01.01.00057
MinMetadataTimestamp = Mon, 24 Oct 2011 12:16:01 GMT
OfficeList = 3637
GetMetadata = /metadata
GetObject = /GETOBJECT
Login = /LOGIN
Logout = /LOGOUT
Search = /SEARCH
</RETS-RESPONSE>
</RETS>

I need to extract the data within the RETS-RESPONSE node and create a map out of it. The data is tab delimited. I did something like this:

Dom.Document doc = new Dom.Document();
doc.load(response.getBody());
Dom.XMLNode root = doc.getRootElement();
for(Dom.XmlNode child : root.getChildren()) {
    System.debug('child text : ' + child.getText());
}

I get a long string where it is impossible to determine where one key value pair ended and other began.

Is there any way to read the data from such XML node?

Thank you.

  • Does that XML come from a salesforce API ? IT's not entirely clear how this question is related to salesforce. – Samuel De Rycke Jun 8 '15 at 16:52
  • 1
    No the XML does not come from Salesforce API. But I am trying to use XML class to read the data within the node. – codeinprogress Jun 8 '15 at 17:10
  • 1
    That XML is a poorly designed API. I'd complain if I were you. – sfdcfox Jun 9 '15 at 5:42
2

Code something like this will create a map assuming the text is separated across lines:

Map<String, String> m = new Map<String, String();
for (String line : child.getText().split('\n', -1)) {
    String[] parts = line.split('=', 2);
    if (parts.size() == 2) {
        m.put(parts[0].trim(), parts[1].trim());
    }
}

though the hard work would be to parse things like the timestamps if you have to.

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.