Class Document Stay organized with collections Save and categorize content based on your preferences.
Page Summary
A Document represents an XML document.
It includes methods for adding, getting, removing, and cloning content nodes that are immediate children or descendants of the document.
The Document object allows for managing the root element and Document Type declaration.
Methods are available to check for the presence of a root element and determine the number of immediate child nodes.
A representation of an XML document.
Methods
| Method | Return type | Brief description |
|---|---|---|
add | Document | Appends the given node to the end of the document. |
add | Document | Inserts the given node at the given index among all nodes that are immediate children of thedocument. |
clone | Content[] | Creates unattached copies of all nodes that are immediate children of the document. |
detach | Element | Detaches and returns the document's rootElement node. |
get | Content[] | Gets all nodes that are immediate children of the document. |
get | Content | Gets the node at the given index among all nodes that are immediate children of thedocument. |
get | Integer | Gets the number of nodes that are immediate children of the document. |
get | Content[] | Gets all nodes that are direct or indirect children of the document, in the order theyappear in the document. |
get | Doc | Gets the document'sDoc declaration. |
get | Element | Gets the document's rootElement node. |
has | Boolean | Determines whether the document has a rootElement node. |
remove | Content[] | Removes all nodes that are immediate children of the document. |
remove | Boolean | Removes the given node, if the node is an immediate child of the document. |
remove | Content | Removes the node at the given index among all nodes that are immediate children of thedocument. |
set | Document | Sets the document'sDoc declaration. |
set | Document | Sets the document's rootElement node. |
Detailed documentation
addContent(content)
Appends the given node to the end of the document. Thecontent argument can be aContent object or any node object that corresponds to a type listed inContent. Note, however, that a document can only have one childElementnode, which is implicitly the rootElement node.
Parameters
| Name | Type | Description |
|---|---|---|
content | Content | the node to append |
Return
Document — the document, for chaining
addContent(index, content)
Inserts the given node at the given index among all nodes that are immediate children of thedocument. Thecontent argument can be aContent object or any node objectthat corresponds to a type listed inContent. Note, however, that a documentcan only have one childElement node, which is implicitly the rootElementnode.
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 | Content | the node to insert |
Return
Document — the document, for chaining
cloneContent()
Creates unattached copies of all nodes that are immediate children of the document.
Return
Content[] — an array of unattached copies of all nodes that are immediate children of the document
detachRootElement()
getAllContent()
Gets all nodes that are immediate children of the document.
Return
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 thedocument. If there is no node at the given index, this method returnsnull.
Parameters
| Name | Type | Description |
|---|---|---|
index | Integer | the index for the node among all nodes that are immediate children of the document |
Return
Content — the node, ornull 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 theyappear in the document.
Return
Content[] — an array of all nodes that are direct or indirect children of the document
getDocType()
getRootElement()
hasRootElement()
Determines whether the document has a rootElement node.
Return
Boolean —true if the document has a rootElement node;false if not
removeContent()
Removes all nodes that are immediate children of the document.
Return
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. Thecontent argument can be aContent object or any node object that corresponds to atype listed inContent.
Parameters
| Name | Type | Description |
|---|---|---|
content | 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 thedocument. If there is no node at the given index, this method returnsnull.
Parameters
| Name | Type | Description |
|---|---|---|
index | Integer | the index for the node among all nodes that are immediate children of the document |
Return
Content — the node that was removed, ornull if there is no node at the given index
setDocType(docType)
Sets the document'sDoc declaration. If the document already has a differentDoc node, this method overwrites the old node. This method throws an exception ifthe document already contains the sameDoc node that is being set.
Parameters
| Name | Type | Description |
|---|---|---|
doc | Doc | theDocument to set |
Return
Document — the document, for chaining
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.