---
schema_version: "1"
id: feaf74406e8d70d606e6f938012c96cb9842ae44ddcaf17aa36cb0e8317a4f6c
title: Document Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_classes_xml_dom_document.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 44f1fc41ea6f5f0f986f5db6b2b32ec1297cd98a51abc8203cb193c99f7ba40e
description: Use the Document class to process XML content. You can parse nested XML content that’s up to 50 nodes deep.Creates a new instance of the Dom.Document class.Creates the top-level root element for a document.Returns the top-level root element node in the document. If this method returns null, the root element has not been created yet.Parse the XML representation of the document specified in the xml argument and load it into a document.Returns the XML representation of the document as a String.
---
# Document Class

Use the Document class to process XML content. You can parse nested XML content that’s up to 50 nodes deep.

## Namespace

[Dom](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_Dom.htm "The Dom namespace provides classes and methods for parsing and creating XML content.")

## Usage

One common application is to use it to create the body of a request for [HttpRequest](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httprequest.htm#apex_classes_restful_http_httprequest "Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PATCH, PUT, and DELETE.") or to parse a response accessed by [HttpResponse](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_classes_restful_http_httpresponse "Use the HttpResponse class to handle the HTTP response returned by the Http class.").

- **[Document Constructors](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_constructors)**
- **[Document Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_methods)**

#### See Also

- [Reading and Writing XML Using the DOM](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_xml_dom.htm)

## Document Constructors

The following are constructors for Document.

- **[Document()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_ctor)**  
    Creates a new instance of the Dom.Document class.

### Document()

Creates a new instance of the Dom.Document class.

#### Signature

public Document()

## Document Methods

The following are methods for Document. All are instance methods.

- **[createRootElement(name, namespace, prefix)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_createRootElement)**  
    Creates the top-level root element for a document.
- **[getRootElement()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_getRootElement)**  
    Returns the top-level root element node in the document. If this method returns null, the root element has not been created yet.
- **[load(xml)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_load)**  
    Parse the XML representation of the document specified in the xml argument and load it into a document.
- **[toXmlString()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_document.htm#apex_Dom_Document_toXmlString)**  
    Returns the XML representation of the document as a String.

### createRootElement(name, namespace, prefix)

Creates the top-level root element for a document.

#### Signature

public Dom.XmlNode createRootElement(String name, String namespace, String prefix)

#### Parameters

- **name**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **namespace**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **prefix**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Dom.XmlNode](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_xmlnode.htm#apex_classes_xml_dom_xmlnode "Use the XmlNode class to work with a node in an XML document.")

#### Usage

For more information about namespaces, see [Reading and Writing XML Using the DOM](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_xml_dom.htm).

Calling this method more than once on a document generates an error as a document can have only one root element.

### getRootElement()

Returns the top-level root element node in the document. If this method returns null, the root element has not been created yet.

#### Signature

public Dom.XmlNode getRootElement()

#### Return Value

Type: [Dom.XmlNode](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_dom_xmlnode.htm#apex_classes_xml_dom_xmlnode "Use the XmlNode class to work with a node in an XML document.")

### load(xml)

Parse the XML representation of the document specified in the xml argument and load it into a document.

#### Signature

public Void load(String xml)

#### Parameters

- **xml**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: Void

#### Example

Dom.Document doc = new Dom.Document();
doc.load(xml);

### toXmlString()

Returns the XML representation of the document as a String.

#### Signature

public String toXmlString()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")