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.
Methods
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. |
Example:
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();
//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()) {
System.debug(child.getText());
}
}
}
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 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:
- 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
http://www.xmlmaster.org/en/article/d01/c10/
91BB371460
ReplyDeleteTakipçi Satın Al
En İyi Bilim Kurgu Filmleri
Aşk Kitapları
9C943ABF
ReplyDeleteGümüşhane
Batman
Siirt
Edirne
Burdur
Bitlis
Tekirdağ
Urfa
Van
47F80524
ReplyDeleteSaray
Döşemealtı
Bayraklı
Karabağlar
Araban
İslahiye
Yüreğir
Kayapınar
Alaşehir
980D200E
ReplyDeleteBeyoğlu
Belek
Dilovası
Bozüyük
Osmangazi
Keşan
Gaziemir
Çukurambar
Selçuklu
62CD3F81
ReplyDeleteAvşa
Bağlar
Kemalpaşa
Tire
Milas
Polatlı
Nusaybin
Bodrum
Urla