XML technologies / XML / XML in a Nutshell / DOM reference / Object reference / Document

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

The interface Document represents a complete, well-formed XML document. Once the Document object is created through the DOMImplementation interface, its parts can be accessed using methods of the Node interface, from which the Document interface also derives.

In DOM, elements and other node types exist only within a document. They can therefore only be created using methods of the Document interface.

 

Attributes

The following attributes are defined for the Document object:

 

doctype: DocumentType

Returns an instance of the DocumentType interface representing this document’s document type declaration. If the document has no DOCTYPE declaration, this property is null. Before DOM Level 3, the document’s DocumentType node could not be changed or directly created. In Level 3, the attribute doctype is a shortcut to the DocumentType node currently in the document hierarchy. Read-only.

Java binding
public DocumentType getDoctype( );

Java example

// Lies die DTD des Dokuments.
DocumentType docType = docIn.getDoctype(  );

if (docType == null) {
    System.out.println("Warnung: Es wurde keine DTD angegeben.");
}

 

documentElement: Element

A reference to the Element node, the XML document tree’s root element. Read-only.

Java binding
public Element getDocumentElement( );

Java example

// Finde das Wurzelelement.
Element elRoot = docIn.getDocumentElement(  );
System.out.println("Der Dokumenttyp ist '" + elRoot.getTagName(  ) + "'
                   document.");

 

documentURI: DOMString(3)

The document location, or null if created using the createDocument( ) method of the DOMImplementation interface. Setting this parameter performs no lexical URI check.

Java binding
public String getDocumentURI( ); public void setDocumentURI(string documentURI);

 

domConfig: DOMConfiguration(3)

Returns the DOMConfiguration object instance associated with this document. DOMConfiguration controls the operations of normalizeDocument( ). For the full parameter list and their effects on normalizeDocument( ), see the DOMConfiguration interface description. Read-only.

Java binding
public DOMConfiguration getDomConfig( );

 

implementation: DOMImplementation

Returns a reference to the DOMImplementation interface instance that created this document. DOM objects from several implementations may be combined, for example Adobe’s SVG plugin in Microsoft Internet Explorer. Read-only.

Java binding
public DOMImplementation getImplementation( );

Java example

// Ist unsere Implementierung konform mit DOM Level 1 XML?
DOMImplementation di = doc.getImplementation(  );
if (!di.hasFeature("XML", "1.0")) {
    return false;
}

 

inputEncoding: DOMString(3)

The character encoding detected when parsing. For more on encodings, see the pages under Internationalization. It is null if unknown. Read-only.

Java binding
public String getInputEncoding( );

 

strictErrorChecking: boolean(3)

If set to false, implementations may ignore errors, such as invalid identifier characters, that would normally raise a DOMException. Even without exceptions, behavior after an error is undefined. The default is true.

Java binding
public boolean getStrictErrorChecking( ); public void setStrictErrorChecking(boolean strictErrorChecking);

 

xmlEncoding: DOMString(3)

Returns the character encoding specified in the encoding pseudo-attribute of the original document’s XML declaration. It is null if unknown. Read-only.

Java binding
public String getXmlEncoding( );

 

xmlStandalone: boolean(3)

Returns the value of the standalone pseudo-attribute in the original XML declaration, or false if absent. This represents the original declaration’s standalone value and may itself be false.

Java binding
public boolean getXmlStandalone( ); public void setXmlStandalone(boolean xmlStandalone) throws DOMException;

 

xmlVersion: DOMString(3)

Returns the value of the version pseudo-attribute in the XML declaration. Without a declaration, it defaults to "1.0". Changing it to "1.1" changes how methods behave under the XML 1.1 standard, including createElement( ), setAttribute( ) and others checking XML names for illegal characters. For differences between XML 1.0 and 1.1, see XML basics and the XML reference.

Java binding
public String getXmlVersion( ); public void setXmlVersion(string xmlVersion) throws DOMException;

 

Methods

The following methods are defined for Document objects:

 

adoptNode: adoptNode, source(3)

Like importNode( ), this method moves a DOM Node from one Document instance to another. It removes the source node from its parent document and prepares it for insertion into the target. In contrast, importNode( ) only copies the source, leaving the original in place. The following table explains behavior for each node type:

Knotentyp

Ergebnis

ATTRIBUTE_NODE

Adoptiert das Quellattribut und alle seine Kinder. Das Attribut ownerElement wird auf null gesetzt und das specified-Flag auf true.

DOCUMENT_FRAGMENT_NODE

Adoptiert den DocumentFragment-Knoten gemeinsam mit all seinen Kindern.

DOCUMENT_NODE

Kann nicht adoptiert werden.

DOCUMENT_TYPE_NODE

Kann nicht adoptiert werden.

ELEMENT_NODE

Adoptiert das Element und die Kindknoten. Adoptiert die Attributknoten, deren specified-Flag gesetzt ist, und kann auf Basis der DTD oder des Schemas für das Zieldokument zusätzliche Attribute einfügen.

ENTITY_NODE

Kann nicht adoptiert werden.

ENTITY_REFERENCE_NODE

Adoptiert nur den EntityReference-Knoten. Sein Wert, wenn einer vorhanden ist, wird aus der DTD des Dokuments genommen, das die Adoption durchführt.

NOTATION_NODE

Kann nicht adoptiert werden.

PROCESSING_INSTRUCTION_ NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE

Werden alle ohne Einschränkungen adoptiert.

Since the target did not create the adopted node, element and attribute names may not match its XML version; see the attribute xmlVersion. Consider using normalizeDocument( ) to ensure adopted nodes are well-formed.

Argument

  • source: Node

    The node to adopt.

Exceptions

  • NOT_SUPPORTED_ERR

    Raised when attempting to adopt an unsupported node type, such as a Document node.

  • NO_MODIFICATION_ALLOWED_ERR

    Raised if the source node is read-only.

Java binding
public Node adoptNode(Node source) throws DOMException;

 

createAttribute: name

Creates a new Attr object with the specified name. Nodes of type Attr are complex objects that can themselves have children such as EntityReference or Text objects.

Argument

Return value
The new Attr object.

Exception

  • INVALID_CHARACTER_ERR

    Indicates that the name passed to createAttribute( ) is not a valid XML name. 

Java binding
public Attr createAttribute(String name) throws DOMException;

Java example

// Eine Entity-Referenz wird erzeugt.
EntityReference er = doc.createEntityReference("entity_name");

// Bevor wir die Entity-Referenz explizit einbinden, müssen
// wir erst ein Attribut erzeugen.
Attr attr = doc.createAttribute("name");

// Jetzt können wir die Entity-Referenz an das Attribut anhängen.
attr.appendChild(er);

 

createAttributeNS: namespaceURI, qualifiedName(2)

Serves the same purpose as createAttribute, but is used when the attribute belongs to a namespace.

Arguments

  • namespaceURI: DOMString

    The URI associated with the namespace prefix in the parameter qualifiedName.

  • qualifiedName: DOMString

    The attribute name, including the namespace prefix associated with the URI in namespaceURI.

Return value

The new Attr object, with the following attribute values:

Attribut

Wert

Node.nodeName

Der vollständig qualifizierte Name aus dem Parameter qualifiedName.

Node.namespaceURI

Der URI des angegebenen Namensraums.

Node.prefix

Das Präfix des Namensraums, der aus dem Parameter qualifiedName gelesen wurde.

Node.localName

Der lokale Teil des vollständig qualifizierten Namens, d.h. der rechts vom :-Zeichen stehende Teil.

Attr.name

Der qualifizierte Name.

Exceptions

  • INVALID_CHARACTER_ERR

    Indicates that the name in createAttributeNS( ) is not a valid XML name.

  • NAMESPACE_ERR

    Raised if qualifiedName is not well-formed, if a prefix is supplied without a namespaceURI, or if the reserved prefix xml is misused.

Java binding
public Attr createAttributeNS(String namespaceURI, String qualifiedName)             throws DOMException;

 

createCDATASection: data

Creates a new CDATASection node with the supplied text.

Argument

  • data: DOMString

    The text to embed in the new CDATASection object.

Exception

  • NOT_SUPPORTED_ERR

    Raised when called on an HTML document, since HTML has no CDATA sections.

Java binding
public CDATASection createCDATASection(String data) throws DOMException;

Java example

// Wir verwenden eine CDATASection, um XML-Sonderzeichen einzubetten.
CDATASection cds = doc.createCDATASection("
<xml_beispiel>Beispieltext.</xml_beispiel>
");

 

createComment: data

Returns a new Comment node with the supplied comment text. For restrictions on Comment nodes, see the object reference entry for Comment.

Argument

  • data: DOMString

    The comment text.

Restrictions on comment text

XML forbids even the characters -- inside comments. Some DOM implementations nevertheless fail to reject double hyphens in comments as syntax errors.

Java binding
public Comment createComment(String data);

Java example

// Füge Datum und Uhrzeit als Kommentar ein.
StringBuffer sb = new StringBuffer(  );
Date dtNow = new Date(  );
sb.append("\tLetzte Änderung " + dtNow.toString(  ) + '\n');
Comment cmt = doc.createComment(sb.toString(  ));

 

createDocumentFragment( )

Creates an empty DocumentFragment object. Its uses are described further under DocumentFragment.

Java binding
public DocumentFragment createDocumentFragment( );

 

createElement: tagName

Creates a new, empty Element node for the parent document, taking its name as an argument. The resulting Element node belongs to its creating Document object, but is not yet in the tree. For methods manipulating the document hierarchy, see the reference section Node.

Argument

  • tagName: DOMString

    The XML name for the new Element node. It is assigned to the nodeName attribute of the resulting Element node.

Return value

The new Element object.

Exception

  • INVALID_CHARACTER_ERR

    Indicates that the name passed to createElement( ) is not a valid XML name.

Java binding
public Element createElement(String tagName) throws DOMException;

Java example

// Erzeuge das neue Element mein_tag.
Element elOut = doc.createElement("mein_tag");

 

createElementNS: namespaceURI, qualifiedName(2)

Serves the same purpose as createElement, but is used for an element in a namespace.

Arguments

  • namespaceURI: DOMString

    The namespace URI.

  • qualifiedName: DOMString

    The new element’s tag name, including a namespace prefix associated with the URI in namespaceURI.

Return value

A newly created Element object with the following attribute values:

Attribut

Wert

Node.nodeName

Der vollständig qualifizierte Name aus dem Parameter qualifiedName.

Node.namespaceURI

Der URI des Namensraums.

Node.prefix

Das aus dem Parameter qualifiedName abgeleitete Präfix des Namensraums.

Node.localName

Der lokale Teil des Tag-Namens, d.h. der nach dem : kommende Teil des nodeName.

Element.tagName

Der voll qualifizierte Tag-Name des Elements, d.h. derselbe wie der qualifiedName.

Exceptions

  • INVALID_CHARACTER_ERR

    Raised if the name passed to createElementNS( ) is not a valid XML name.

  • NAMESPACE_ERR

    Raised if qualifiedName is not well-formed, if a prefix is supplied without a namespaceURI, or if the reserved prefix xml is used.

Java binding
public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

 

createEntityReference: name

Creates an EntityReference object.

Argument

  • name: DOMString

    The referenced XML entity’s name. It must be defined elsewhere in this XML document by an entity declaration.

Exceptions

  • INVALID_CHARACTER_ERR

    Raised if the name passed to createEntityReference( ) is not a valid XML name.

  • NOT_SUPPORTED_ERR

    Raised when creating an entity reference in HTML, which permits none.

Java binding
public EntityReference createEntityReference(String name) throws DOMException;

Java example

// Erzeuge eine Entity-Referenz.
EntityReference er = doc.createEntityReference("entity_name");

 

createProcessingInstruction: target, data

Creates a new ProcessingInstruction node with the supplied target name and data values. The special target “xml”, in any case combination, is reserved and must not be used.

Arguments

  • target: DOMString

    The target name of the processing instruction.

  • data: DOMString

    Application-specific data passed to the newly created ProcessingInstruction node.

Exceptions

  • INVALID_CHARACTER_ERR

    Indicates that the name passed to createProcessingInstruction is not a valid XML name.

  • NOT_SUPPORTED_ERR

    Raised when creating a ProcessingInstruction in HTML, where processing instructions are forbidden.

Java binding
public ProcessingInstruction createProcessingInstruction(String target, String data) throws DOMException;

Java example

// Erzeuge eine anwendungsspezifische Verarbeitungsanweisung.
ProcessingInstruction pi = doc.createProcessingInstruction("meine_anwendung",
            "action=\"save\"");

 

createTextNode: data

Creates a new Text node containing the supplied data string.

Argument

  • data: DOMString

    The text node’s content.

Java binding
public Text createTextNode(String data);

Java example

// Erzeuge einen neuen Textknoten mit Zeichendaten.
Text txtDesc = doc.createTextNode("Zeichendaten für ein neues Element.");

 

getElementById: elementID(2)

Returns an Element node with an attribute named ID with the supplied value.

Note: Attributes named ID must not be confused with attributes of type ID. ID attributes are declared with the ID type in the document type declaration. For more information, see the XML reference.

Argument

  • elementID: DOMString

    The desired element’s unique ID.

Return value

One Element object with the specified ID attribute, or null if none matches.

Java binding
public Element getElementById(String elementId);

 

getElementsByTagName: tagName

Returns a list of Element nodes in the current document whose tagName attribute matches the tagName parameter, in input document order.

Argument

  • tagName: DOMString

    The desired tag name. The special name * matches all tags.

Java binding
public NodeList getElementsByTagName(String tagName);

Java example

// Erstelle eine Liste aller Telefonnummern im aktuellen Dokument.
NodeList nl = doc.getElementsByTagName("telefon_nummer");

 

getElementsByTagNameNS: namespaceURI, localName(2)

Like getElementsByTagName( ), returns a list of Element nodes, a NodeList-object, matching the criteria specified by namespaceURI and localName. The list contains all elements matching the namespace URI and local name, in input document order.

Arguments

  • namespaceURI: DOMString

    The namespace URI in which to search. The special value * matches any namespace.

  • localName: DOMString

    The desired local name. The special value * matches any local name.

Java binding
public NodeList getElementsByTagNameNS(String namespaceURI, String localName);

 

importNode: importedNode, deep(2)

The name is somewhat misleading: this method copies a Node object from another document. The copy can be inserted into the current document’s tree. Copying details depend on node type, as the following table describes.

Knotentyp

Ergebnis

Effekt des Flags deep

ATTRIBUTE_NODE

Kopiert das Eingabeattribut und alle seine Kinder. Das Attribut ownerElement wird auf null gesetzt und das Flag specified auf true.

Kein Effekt.

DOCUMENT_FRAGMENT_NODE

Erzeugt einen leeren DocumentFragment-Knoten.

Die Kindknoten des zu importierenden DocumentFragment-Knotens werden mitkopiert.

DOCUMENT_NODE

Kann nicht importiert werden.

Nicht anwendbar.

DOCUMENT_TYPE_NODE

Kann nicht importiert werden.

Nicht anwendbar.

ELEMENT_NODE

Kopiert die Attributknoten, deren specified-Flag auf das neue Element gesetzt wurde.

Die Kindknoten des zu importierenden Elements werden rekursiv mitkopiert.

ENTITY_NODE

Kopiert die Attribute publicId, systemId und notationName.

Die Kindknoten des zu importierenden Entity-Knotens werden rekursiv mitkopiert.

ENTITY_REFERENCE_NODE

Kopiert nur den EntityReference-Knoten. Sein Wert wird der DTD des zu importierenden Dokuments entnommen, sofern ein Wert angegeben ist.

Kein Effekt.

NOTATION_NODE

Importiert den notation-Knoten; allerdings ist seit DOM Level 2 das Interface DocumentType nur-lesbar, d.h., der Knoten kann nicht in das Zieldokument eingefügt werden.

Kein Effekt.

PROCESSING_INSTRUCTION_ NODE

Kopiert die Werte von target und data.

Kein Effekt.

TEXT_NODE,CDATA_SECTION_NODE, COMMENT_NODE

Kopiert die Werte der Attribute data und length.

Kein Effekt.

Returns the new copied node according to the supplied arguments.

Arguments

  • importedNode: Node

    The node to copy and insert into the current document.

  • deep: boolean

    Whether to copy only this node or its entire subtree of children. See the table above for details.

Exception

  • NOT_SUPPORTED_ERR

    Some types, such as Document nodes, cannot be imported; attempting it raises this exception.

Java binding
public Node importNode(Node importedNode, boolean deep) throws DOMException;

 

normalizeDocument( ) (3)

This performs the equivalent of loading and saving, returning the document in normalized form. Text nodes are merged, possible EntityReference nodes are expanded, and so on. Parameters in the DOMConfiguration object, see the domConfig attribute, control the operations performed. For standard configuration parameters, see the interface DOMConfiguration.

Java binding
public void normalizeDocument( );

 

renameNode: n, namespaceURI, qualifiedName(3)

Renames nodes of types Element and Attr. The target nodeName attribute of the Node is changed directly where possible. Otherwise a node with the new name is created, and the original node’s children move to it. For an Element node, renaming removes defaults associated with the old name and supplies those associated with the new name.

Arguments

  • n: Node

    The node to rename.

  • namespaceURI: DOMString

    The renamed node’s namespace URI.

  • qualifiedName: DOMString

    The node’s new qualified name.

Exceptions

  • NOT_SUPPORTED_ERR

    Raised if the target nodeType is not ELEMENT_NODE or ATTRIBUTE_NODE, or the implementation cannot rename the documentElement.

  • INVALID_CHARACTER_ERR

    Raised if the new qualified name is not a valid XML name under the version specified by xmlVersion.

  • WRONG_DOCUMENT_ERR

    Raised when trying to rename a node from another document.

  • NAMESPACE_ERR

    Raised if qualifiedName is malformed, or has a prefix without a namespaceURI, or incorrectly uses the reserved prefix xml.

Java binding
public Node renameNode(Node n, String namespaceURI, String qualifiedName) throws DOMException;

  

Back to top

 

 

 

back to top