Skip to main content

XML DOM: Document Object and DocumentType Object

Document Object

The Document object represents the entire XML document.

It is the root of an XML document tree, and gives us the primary access to the document's data.

Document Object Properties

PropertyDescription
childNodesReturns a NodeList of child nodes for the document
doctypeReturns the Document Type Declaration associated with the document
documentElementReturns the root node of the document
documentURISets or returns the location of the document
firstChildReturns the first child node of the document
implementationReturns the DOMImplementation object that handles this document
inputEncodingReturns the encoding used for the document (when parsing)
lastChildReturns the last child node of the document
nodeNameReturns the name of a node (depending on its type)
nodeTypeReturns the node type of a node
nodeValueSets or returns the value of a node (depending on its type)
xmlEncodingReturns the XML encoding of the document
xmlStandaloneSets or returns whether the document is standalone
xmlVersionSets or returns the XML version of the document

Document Object Methods

MethodDescription
adoptNode(sourcenode)Adopts a node from another document to this document, and returns the adopted node
createAttribute(name)Creates an attribute node with the specified name, and returns the new Attr object
createAttributeNS(uri,name)Creates an attribute node with the specified name and namespace, and returns the new Attr object
createCDATASection()Creates a CDATA section node
createComment()Creates a comment node
createDocumentFragment()Creates an empty DocumentFragment object, and returns it
createElement()Creates an element node
createElementNS()Creates an element node with a specified namespace
createEntityReference(name)Creates an EntityReference object, and returns it
createProcessingInstruction(target,data)Creates a ProcessingInstruction object, and returns it
createTextNode()Creates a text node
getElementById(id)Returns the element that has an ID attribute with the given value. If no such element exists, it returns null
getElementsByTagName()Returns a NodeList of all elements with a specified name
getElementsByTagNameNS()Returns a NodeList of all elements with a specified name and namespace
importNode(nodetoimport,deep)Imports a node from another document to this document. This method creates a new copy of the source node. If the deep parameter is set to true, it imports all children of the specified node. If set to false, it imports only the node itself. This method returns the imported node
normalizeDocument()
renameNode()Renames an element or attribute node

DocumentType Object

The DocumentType objects are the key to access the document's data and in the document, the doctype attribute can have either the null value or the DocumentType Object value. These DocumentType objects act as an interface to the entities described for an XML document.

DocumentType Object Properties

PropertyDescription
nameIt returns the name of the DTD which is written immediately next to the keyword !DOCTYPE.
entitiesIt returns a NamedNodeMap object containing the general entities, both external and internal, declared in the DTD.
notationsIt returns a NamedNodeMap containing the notations declared in the DTD.
internalSubsetIt returns an internal subset as a string, or null if there is none.
publicIdIt returns the public identifier of the external subset.
systemIdIt returns the system identifier of the external subset. This may be an absolute URI or not.

DocumentType Object Methods

DocumentType inherits methods from its parent, Node, and implements the ChildNode interface.