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.
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 http://my.name.space and a prefix of myprefix.
<sampleElement xmlns:myprefix="http://my.name.space" />
In the following example, the XML element has two attributes:
<square dimension="2" ns1:foo="ns2:bar" xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" />
The Document class has the following methods:
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| createRootElement | String name | Dom.XmlNode | Creates 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 null, Salesforce 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. |
| getRootElement | Dom.XmlNode | Returns the top-level root element node in the document. If this method returns null, the root element has not been created yet. | |
| load | String xml | Void | Parse 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();
doc.load(xml); |
| toXmlString | String | Returns 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:
<address> <name>Kirk Stevens</name> <street1>808 State St</street1> <street2>Apt. 2</street2> <city>Palookaville</city> <state>PA</state> <country>USA</country> </address>
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 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()); } } }