Document

A representation of an XML document.

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/xml-service/document#addContent(Content)` | `https://developers.google.com/apps-script/reference/xml-service/document#` | Appends the given node to the end of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#addContent(Integer,Content)` | `https://developers.google.com/apps-script/reference/xml-service/document#` | Inserts the given node at the given index among all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#cloneContent()` | `https://developers.google.com/apps-script/reference/xml-service/content` | Creates unattached copies of all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#detachRootElement()` | `https://developers.google.com/apps-script/reference/xml-service/element|null` | Detaches and returns the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getAllContent()` | `https://developers.google.com/apps-script/reference/xml-service/content` | Gets all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getContent(Integer)` | `https://developers.google.com/apps-script/reference/xml-service/content|null` | Gets the node at the given index among all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getContentSize()` | `Integer` | Gets the number of nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getDescendants()` | `https://developers.google.com/apps-script/reference/xml-service/content` | Gets all nodes that are direct or indirect children of the document, in the order they appear in the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getDocType()` | `https://developers.google.com/apps-script/reference/xml-service/doc-type|null` | Gets the document's `https://developers.google.com/apps-script/reference/xml-service/doc-type` declaration. |
| `https://developers.google.com/apps-script/reference/xml-service/document#getRootElement()` | `https://developers.google.com/apps-script/reference/xml-service/element|null` | Gets the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. |
| `https://developers.google.com/apps-script/reference/xml-service/document#hasRootElement()` | `Boolean` | Determines whether the document has a root `https://developers.google.com/apps-script/reference/xml-service/element` node. |
| `https://developers.google.com/apps-script/reference/xml-service/document#removeContent()` | `https://developers.google.com/apps-script/reference/xml-service/content` | Removes all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#removeContent(Content)` | `Boolean` | Removes the given node, if the node is an immediate child of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#removeContent(Integer)` | `https://developers.google.com/apps-script/reference/xml-service/content|null` | Removes the node at the given index among all nodes that are immediate children of the document. |
| `https://developers.google.com/apps-script/reference/xml-service/document#setDocType(DocType)` | `https://developers.google.com/apps-script/reference/xml-service/document#` | Sets the document's `https://developers.google.com/apps-script/reference/xml-service/doc-type` declaration. |
| `https://developers.google.com/apps-script/reference/xml-service/document#setRootElement(Element)` | `https://developers.google.com/apps-script/reference/xml-service/document#` | Sets the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. |

## Detailed documentation

### `addContent(content)`

Appends the given node to the end of the document. The `content` argument can be a `https://developers.google.com/apps-script/reference/xml-service/content` object or any node object that corresponds to a type listed in `https://developers.google.com/apps-script/reference/xml-service/content-types`. Note, however, that a document can only have one child `https://developers.google.com/apps-script/reference/xml-service/element`
node, which is implicitly the root `Element` node.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `content` | `https://developers.google.com/apps-script/reference/xml-service/content` | The node to append. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/document#` --- The document, for chaining.

*** ** * ** ***

### `addContent(index, content)`

Inserts the given node at the given index among all nodes that are immediate children of the
document. The `content` argument can be a `https://developers.google.com/apps-script/reference/xml-service/content` object or any node object
that corresponds to a type listed in `https://developers.google.com/apps-script/reference/xml-service/content-types`. Note, however, that a document
can only have one child `https://developers.google.com/apps-script/reference/xml-service/element` node, which is implicitly the root `Element`
node.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `index` | `Integer` | The index at which to insert the node among all nodes that are immediate children of the document. |
| `content` | `https://developers.google.com/apps-script/reference/xml-service/content` | The node to insert. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/document#` --- The document, for chaining.

*** ** * ** ***

### `cloneContent()`

Creates unattached copies of all nodes that are immediate children of the document.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content` --- An array of unattached copies of all nodes that are immediate children of the
document.

*** ** * ** ***

### `detachRootElement()`

Detaches and returns the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. If the document does not have
a root `Element` node, this method returns `null`.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/element|null` --- The detached `Element` node, or `null` if the document does not have a root
`Element` node.

*** ** * ** ***

### `getAllContent()`

Gets all nodes that are immediate children of the document.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content` --- An array of all nodes that are immediate children of the document.

*** ** * ** ***

### `getContent(index)`

Gets the node at the given index among all nodes that are immediate children of the
document. If there is no node at the given index, this method returns `null`.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `index` | `Integer` | The index for the node among all nodes that are immediate children of the document. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content|null` --- The node, or `null` if there is no node at the given index.

*** ** * ** ***

### `getContentSize()`

Gets the number of nodes that are immediate children of the document.

#### Return


`Integer` --- The number of nodes that are immediate children of the document.

*** ** * ** ***

### `getDescendants()`

Gets all nodes that are direct or indirect children of the document, in the order they
appear in the document.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content` --- An array of all nodes that are direct or indirect children of the document.

*** ** * ** ***

### `getDocType()`

Gets the document's `https://developers.google.com/apps-script/reference/xml-service/doc-type` declaration. If the document does not have a `DocumentType` node, this method returns `null`.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/doc-type|null` --- The `DocumentType` node, or `null` if the document does not have a `DocumentType` node.

*** ** * ** ***

### `getRootElement()`

Gets the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. If the document does not have a root `Element` node, this method returns `null`.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/element|null` --- The root `Element` node, or `null` if the document does not have a root
`Element` node.

*** ** * ** ***

### `hasRootElement()`

Determines whether the document has a root `https://developers.google.com/apps-script/reference/xml-service/element` node.

#### Return


`Boolean` --- `true` if the document has a root `Element` node; `false` if not.

*** ** * ** ***

### `removeContent()`

Removes all nodes that are immediate children of the document.

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content` --- An array of all nodes that were immediate children of the document before they
were removed.

*** ** * ** ***

### `removeContent(content)`

Removes the given node, if the node is an immediate child of the document. The `content` argument can be a `https://developers.google.com/apps-script/reference/xml-service/content` object or any node object that corresponds to a
type listed in `https://developers.google.com/apps-script/reference/xml-service/content-types`.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `content` | `https://developers.google.com/apps-script/reference/xml-service/content` | The node to remove. |

#### Return


`Boolean` --- `true` if the node was an immediate child and was removed; `false` if not.

*** ** * ** ***

### `removeContent(index)`

Removes the node at the given index among all nodes that are immediate children of the
document. If there is no node at the given index, this method returns `null`.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `index` | `Integer` | The index for the node among all nodes that are immediate children of the document. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/content|null` --- The node that was removed, or `null` if there is no node at the given index.

*** ** * ** ***

### `setDocType(docType)`

Sets the document's `https://developers.google.com/apps-script/reference/xml-service/doc-type` declaration. If the document already has a different
`DocType` node, this method overwrites the old node. This method throws an exception if
the document already contains the same `DocType` node that is being set.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `docType` | `https://developers.google.com/apps-script/reference/xml-service/doc-type` | The `DocumentType` to set. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/document#` --- The document, for chaining.

*** ** * ** ***

### `setRootElement(element)`

Sets the document's root `https://developers.google.com/apps-script/reference/xml-service/element` node. If the document already has a root `Element` node, this method overwrites the old node.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `element` | `https://developers.google.com/apps-script/reference/xml-service/element` | The root `Element` node to set. |

#### Return


`https://developers.google.com/apps-script/reference/xml-service/document#` --- The document, for chaining.