Class Document

  • 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.

Document

A representation of an XML document.

Methods

MethodReturn typeBrief description
addContent(content)DocumentAppends the given node to the end of the document.
addContent(index, content)DocumentInserts the given node at the given index among all nodes that are immediate children of thedocument.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the document.
detachRootElement()ElementDetaches and returns the document's rootElement node.
getAllContent()Content[]Gets all nodes that are immediate children of the document.
getContent(index)ContentGets the node at the given index among all nodes that are immediate children of thedocument.
getContentSize()IntegerGets the number of nodes that are immediate children of the document.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the document, in the order theyappear in the document.
getDocType()DocTypeGets the document'sDocType declaration.
getRootElement()ElementGets the document's rootElement node.
hasRootElement()BooleanDetermines whether the document has a rootElement node.
removeContent()Content[]Removes all nodes that are immediate children of the document.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the document.
removeContent(index)ContentRemoves the node at the given index among all nodes that are immediate children of thedocument.
setDocType(docType)DocumentSets the document'sDocType declaration.
setRootElement(element)DocumentSets 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 inContentType. Note, however, that a document can only have one childElementnode, which is implicitly the rootElement node.

Parameters

NameTypeDescription
contentContentthe 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 inContentType. Note, however, that a documentcan only have one childElement node, which is implicitly the rootElementnode.

Parameters

NameTypeDescription
indexIntegerthe index at which to insert the node among all nodes that are immediate children of the document
contentContentthe 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()

Detaches and returns the document's rootElement node. If the document does not havea rootElement node, this method returnsnull.

Return

Element — the detachedElement node, ornull if the document does not have a rootElement node


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

NameTypeDescription
indexIntegerthe 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()

Gets the document'sDocType declaration. If the document does not have aDocumentType node, this method returnsnull.

Return

DocType — theDocumentType node, ornull if the document does not have aDocumentType node


getRootElement()

Gets the document's rootElement node. If the document does not have a rootElement node, this method returnsnull.

Return

Element — the rootElement node, ornull if the document does not have a rootElement node


hasRootElement()

Determines whether the document has a rootElement node.

Return

Booleantrue 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 inContentType.

Parameters

NameTypeDescription
contentContentthe node to remove

Return

Booleantrue 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

NameTypeDescription
indexIntegerthe 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'sDocType declaration. If the document already has a differentDocType node, this method overwrites the old node. This method throws an exception ifthe document already contains the sameDocType node that is being set.

Parameters

NameTypeDescription
docTypeDocTypetheDocumentType to set

Return

Document — the document, for chaining


setRootElement(element)

Sets the document's rootElement node. If the document already has a rootElement node, this method overwrites the old node.

Parameters

NameTypeDescription
elementElementthe rootElement node 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.