1

I want to get the task Id from the below response.

<?xml version="1.0" encoding="utf-8"?>
<response>
    <task id="56639c84-7f73-4ace-b7ab-a46683ff0e25" registrationTime="2017-06-07T13:46:11Z" statusChangeTime="2017-06-07T13:46:11Z" status="Queued" filesCount="1" credits="10" estimatedProcessingTime="5" />
</response>

Anyone help me how can i get the above task id in apex?

1
  • 1
    What have you tried so far? Have you done any research? There are plenty of similar questions that should point you in the right direction.
    – Derek F
    Jun 7 '17 at 15:05
4

You can use the Document and XmlNode classes in the DOM namespace to parse XML.

String xml = '<?xml version="1.0" encoding="utf-8"?><response><task id="56639c84-7f73-4ace-b7ab-a46683ff0e25" registrationTime="2017-06-07T13:46:11Z" statusChangeTime="2017-06-07T13:46:11Z" status="Queued" filesCount="1" credits="10" estimatedProcessingTime="5" /></response>';
DOM.Document document = new DOM.Document();
document.load(xml);
String taskId = document.getRootElement()
    .getChildElement('task', null)
    .getAttribute('id', null);
system.debug(taskId);
0
1

Just load the XML into Dom.Document, then you can get the appropriate attributes:

Dom.Document doc = new Dom.Document();
doc.load(xmlString);
Dom.XmlNode rootNode = doc.getRootElement(),
  taskNode = rootNode.getChildElement('task',null);
String taskId = taskNode.getAttribute('id',null),
       taskStatus = taskNode.getAttribute('status',null);
DateTime registrationTime = (DateTime)JSON.serialize(taskNode.getAttribute('registrationTime',null), DateTime.class));
Integer filesCount = Integer.valueOf(taskNode.getAttribute('filesCount',null)),
    credits = Integer.valueOf(taskNode.getAttribute('credits',null)),
    estimatedProcessingTime = Integer.valueOf(taskNode.getAttribute('estimatedProcessingTime',null));

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.