Skip to content

XML DOM: Document Object and DocumentType Object

Explore the Document and DocumentType objects in XML DOM, including key properties, methods, and how they provide structured access to XML document elements, attributes, and DTD declarations.

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

Property Description
childNodes Returns a NodeList of child nodes for the document
doctype Returns the Document Type Declaration associated with the document
documentElement Returns the root node of the document
documentURI Sets or returns the location of the document
firstChild Returns the first child node of the document
implementation Returns the DOMImplementation object that handles this document
inputEncoding Returns the encoding used for the document (when parsing)
lastChild Returns the last child node of the document
nodeName Returns the name of a node (depending on its type)
nodeType Returns the node type of a node
nodeValue Sets or returns the value of a node (depending on its type)
xmlEncoding Returns the XML encoding of the document
xmlStandalone Sets or returns whether the document is standalone
xmlVersion Sets or returns the XML version of the document

Document Object Methods

Method Description
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

Property Description
name It returns the name of the DTD which is written immediately next to the keyword !DOCTYPE.
entities It returns a NamedNodeMap object containing the general entities, both external and internal, declared in the DTD.
notations It returns a NamedNodeMap containing the notations declared in the DTD.
internalSubset It returns an internal subset as a string, or null if there is none.
publicId It returns the public identifier of the external subset.
systemId It 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.

Navigation