Working on Order Capture has made me explore lots of new Salesforce concepts which were unknown to me. In one of the Apex classes, we were framing HTTP request and also parsing HTTP response using the DOM classes. I was unaware of it. It exhorted me to dive deep into it and share among the other people here on The Hub..
Introduction
The DOM represents an XML document as a hierarchy of nodes. Some nodes may be branch nodes and have child nodes, while others are leaf nodes with no children.Apex provides classes that enable you to work with XML content using the DOM (Document Object Model).DOM classes help you parse or generate XML content.
Common Usage of DOM: to generate the body of a request created by HttpRequest or to parse a response accessed by HttpResponse.
- Use the Document Class to process the content in the body of the XML document.
- Use the XmlNode Class to work with a node in the XML document.
Take an example (Parsing an XML content)
Considering this as the XML response:
The following example illustrates how to use DOM classes to parse the above XML response returned in the body of a GET request:
public class DomDocument {
// Pass in the URL for the request
// For the purposes of this sample,assume that the URL
// returns the XML shown above in the response body
public void parseResponseDom(String url){
Http h = new Http();
HttpRequest req = new HttpRequest();
// url that returns the XML in the response body
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode address = doc.getRootElement();
String name = address.getChildElement('name', null).getText();
String state = address.getChildElement('state', null).getText();
// print out specific elements
System.debug('Name: ' + name);
System.debug('State: ' + state);
// Alternatively, loop through the child elements.
// This prints out all the elements of the address
for(Dom.XMLNode child : address.getChildElements()) {
System.debug(child.getText());
}
}
}
Take an example (Generating an XML content)
public class DomNamespaceSample
{
public void sendRequest(String endpoint)
{
String soapNS = 'http://schemas.xmlsoap.org/soap/envelope/';
String servicePrefix ='glob';
String serviceNS = 'http://www.myservice.com/services/MyService/';
// Create the request envelope
DOM.Document doc = new DOM.Document();
dom.XmlNode envelope = doc.createRootElement('Envelope', soapNS, 'soapenv');
envelope.setNamespace(servicePrefix, serviceNS);
dom.XmlNode body = envelope.addChildElement('Body', soapNS, null);
body.addChildElement('echo', serviceNS, 'req').addChildElement('category', serviceNS, null).addTextNode('classifieds');
System.debug('Request XML:' + doc.toXmlString());
// Send the request
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setHeader('Content-Type', 'text/xml');
req.setBodyDocument(doc);
Http http = new Http();
HttpResponse res = http.send(req);
System.assertEquals(200, res.getStatusCode());
dom.Document resDoc = res.getBodyDocument();
envelope = resDoc.getRootElement();
System.assertEquals('classifieds', envelope.getChildElement('Body', soapNS).getChildElement('echo', serviceNS).getChildElement('category', serviceNS).getText());
}
}
Useful Quicklinks
Happy Apex Coding!
No comments:
Post a Comment
Thank you for visiting. Your comments are highly appreciated.