nu.xom

## Class Document

- Object
- - nu.xom.Node
  - - nu.xom.ParentNode
    - - nu.xom.Document

-     public class Document
      extends ParentNode

  The `Document` class represents a complete XML document including its root element, prolog, and epilog.

  Version:
    1.3.0
  Author:
    Elliotte Rusty Harold

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Document(Document doc)` Creates a copy of this document.
    `Document(Element root)` Creates a new `Document` object with the specified root element.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Document` | `copy()` Returns a complete copy of this document.
    `String` | `getBaseURI()` Returns the absolute URI from which this document was loaded.
    `DocType` | `getDocType()` Returns this document's document type declaration, or null if it doesn't have one.
    `Element` | `getRootElement()` Returns this document's root element.
    `String` | `getValue()` Returns the value of the document as defined by XPath 1.0.
    `Node` | `removeChild(int position)` Removes the child of this document at the specified position.
    `Node` | `removeChild(Node child)` Removes the specified child from this document.
    `void` | `replaceChild(Node oldChild, Node newChild)` Replaces an existing child with a new child node.
    `void` | `setBaseURI(String URI)` Sets the URI from which this document was loaded, and against which relative URLs in this document will be resolved.
    `void` | `setDocType(DocType doctype)` Sets this document's document type declaration.
    `void` | `setRootElement(Element root)` Replaces the current root element with a different root element.
    `String` | `toString()` Returns a string representation of this document suitable for debugging and diagnosis.
    `String` | `toXML()` Returns the actual complete, well-formed XML document as a `String`.

    - ### Methods inherited from class nu.xom.ParentNode

      `appendChild, getChild, getChildCount, indexOf, insertChild`

    - ### Methods inherited from class nu.xom.Node

      `detach, equals, getDocument, getParent, hashCode, query, query`

    - ### Methods inherited from class Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Document

          public Document(Element root)

      Creates a new `Document` object with the specified root element.

      Parameters:
        `root` - the root element of this document
      Throws:
        `NullPointerException` - if `root` is null
        `MultipleParentException` - if `root` already has a parent

    - #### Document

          public Document(Document doc)

      Creates a copy of this document.

      Parameters:
        `doc` - the document to copy
      Throws:
        `NullPointerException` - if `doc` is null

  - ### Method Detail

    - #### getDocType

          public final DocType getDocType()

      Returns this document's document type declaration, or null if it doesn't have one.

      Returns:
        the document type declaration
      See Also:
        `setDocType(nu.xom.DocType)`

    - #### setDocType

          public void setDocType(DocType doctype)

      Sets this document's document type declaration. If this document already has a document type declaration, then it's inserted at that position. Otherwise, it's inserted at the beginning of the document.

      Parameters:
        `doctype` - the document type declaration
      Throws:
        `MultipleParentException` - if `doctype` belongs to another document
        `NullPointerException` - if `doctype` is null

    - #### getRootElement

          public final Element getRootElement()

      Returns this document's root element. This is guaranteed to be non-null.

      Returns:
        the root element

    - #### setRootElement

          public void setRootElement(Element root)

      Replaces the current root element with a different root element.

      Parameters:
        `root` - the new root element
      Throws:
        `MultipleParentException` - if root has a parent
        `NullPointerException` - if root is null

    - #### setBaseURI

          public void setBaseURI(String URI)

      Sets the URI from which this document was loaded, and against which relative URLs in this document will be resolved. Setting the base URI to null or the empty string removes any existing base URI.

      Specified by:
        `setBaseURI` in class `ParentNode`
      Parameters:
        `URI` - the base URI of this document
      Throws:
        `MalformedURIException` - if `URI` is not a legal absolute URI

    - #### getBaseURI

          public final String getBaseURI()

      Returns the absolute URI from which this document was loaded. This method returns the empty string if the base URI is not known; for instance if the document was created in memory with a constructor rather than by parsing an existing document.

      Overrides:
        `getBaseURI` in class `Node`
      Returns:
        the base URI of this document

    - #### removeChild

          public Node removeChild(int position)

      Removes the child of this document at the specified position. Indexes begin at 0 and count up to one less than the number of children of this document. The root element cannot be removed. Instead, use `setRootElement` to replace the existing root element with a different element.

      Overrides:
        `removeChild` in class `ParentNode`
      Parameters:
        `position` - index of the node to remove
      Returns:
        the node which was removed
      Throws:
        `IndexOutOfBoundsException` - if the index is negative or greater than the number of children of this document - 1
        `WellformednessException` - if the index points to the root element

    - #### removeChild

          public Node removeChild(Node child)

      Removes the specified child from this document. The root element cannot be removed. Instead, use `setRootElement` to replace the existing root element with a different element.

      Overrides:
        `removeChild` in class `ParentNode`
      Parameters:
        `child` - node to remove
      Returns:
        the node which was removed
      Throws:
        `NoSuchChildException` - if the node is not a child of this node
        `WellformednessException` - if child is the root element

    - #### replaceChild

          public void replaceChild(Node oldChild,
                                   Node newChild)

      Replaces an existing child with a new child node. If `oldChild` is not a child of this node, then a `NoSuchChildException` is thrown. The root element can only be replaced by another element.

      Overrides:
        `replaceChild` in class `ParentNode`
      Parameters:
        `oldChild` - the node removed from the tree
        `newChild` - the node inserted into the tree
      Throws:
        `MultipleParentException` - if `newChild` already has a parent
        `NoSuchChildException` - if `oldChild` is not a child of this node
        `NullPointerException` - if either argument is null
        `IllegalAddException` - if `newChild` is an attribute or a text node
        `WellformednessException` - if `newChild` `oldChild` is an element and `newChild` is not

    - #### getValue

          public final String getValue()

      Returns the value of the document as defined by XPath 1.0. This is the same as the value of the root element, which is the complete PCDATA content of the root element, without any tags, comments, or processing instructions after all entity and character references have been resolved.

      Specified by:
        `getValue` in class `Node`
      Returns:
        value of the root element of this document

    - #### toXML

          public final String toXML()

      Returns the actual complete, well-formed XML document as a `String`. Significant white space is preserved. Insignificant white space in tags, the prolog, the epilog, and the internal DTD subset is not preserved. Entity and character references are not preserved. The entire document is contained in this one string.

      Specified by:
        `toXML` in class `Node`
      Returns:
        a string containing this entire XML document

    - #### copy

          public Document copy()

      Returns a complete copy of this document.

      Specified by:
        `copy` in class `Node`
      Returns:
        a deep copy of this `Document` object

    - #### toString

          public final String toString()

      Returns a string representation of this document suitable for debugging and diagnosis. This is not the XML representation of this document.

      Overrides:
        `toString` in class `Object`
      Returns:
        a non-XML string representation of this document

Copyright 2002-2026 Elliotte Rusty Harold
elharo@ibiblio.org