Friday, September 20, 2013

Document Class in Salesforce

Document Class:

Use the Document class to process XML content. One common application is to use it to create the body of a request for HttpRequest or to parse a response accessed by HttpResponse.


The Document class has the following methods:
NameArgumentsReturn TypeDescription
createRootElementString name
String namespace
String prefix
Dom.XmlNodeCreates the top-level root element for a document.
The name argument can't have a null value.
If the namespace argument has a non-null value and the prefix argument is null, the namespace is set as the default namespace.
If the prefix argument is nullSalesforce automatically assigns a prefix for the element. The format of the automatic prefix is nsi, where i is a number.
If the prefix argument is '', the namespace is set as the default namespace.
For more information about namespaces, see XML Namespaces.
Calling this method more than once on a document generates an error as a document can have only one root element.
getRootElementDom.XmlNodeReturns the top-level root element node in the document. If this method returns null, the root element has not been created yet.
loadString xmlVoidParse the XML representation of the document specified in the xml argument and load it into a document. For example:
Dom.Document doc = new Dom.Document();
toXmlStringStringReturns the XML representation of the document as a String.


For the purposes of the sample below, assume that the url argument passed into the parseResponseDom method returns this XML response:

    <name>Kirk Stevens</name>
    <street1>808 State St</street1>
    <street2>Apt. 2</street2>

The following example illustrates how to use DOM classes to parse the 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
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();
        //Retrieve the root element for this document.
        Dom.XMLNode address = doc.getRootElement();
        //getChildElement() method is present in XML Node Class
        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()) {
XML NameSpaces:
An XML namespace is a collection of names identified by a URI reference and used in XML documents to uniquely identify element types and attribute names. Names in XML namespaces may appear as qualified names, which contain a single colon, separating the name into a namespace prefix and a local part. The prefix, which is mapped to a URI reference, selects a namespace. The combination of the universally managed URI namespace and the document's own namespace produces identifiers that are universally unique.
The following XML element has a namespace of and a prefix of myprefix.
<sampleElement xmlns:myprefix="" />
  In the following example, the XML element has two attributes:
  • The first attribute has a key of dimension; the value is 2.
  • The second attribute has a key namespace of http://ns1; the value namespace is http://ns2; the key is foo; the value is bar.
<square dimension="2" ns1:foo="ns2:bar" xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" />

 For more information regarding XML Namespaces Please visit the below link

No comments: