Options
All
  • Public
  • Public/Protected
  • All
Menu

Class XmlDocument

Represents an XML document.

A sample XML document is structured as follows:

<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>My page title</title>
    </head>
    <body>
        <h1>Welcome!</h1>
        <p>I hope you enjoy visiting my website.</p>
        <img src="picture.png"/>
    </body>
</html>

Each component of the document, such as the XML declaration, document type definition, and root element, are children of this node.

XmlDocument nodes must have exactly one XmlElement child, which is the document's root element.

XmlDocument nodes can have exactly one XmlDecl and XmlDtd child in that order, so long as they precede the XmlElement node.

XmlDocument nodes can have an unlimited number of XmlComment or XmlProcInst nodes, so long as they follow the XmlDecl node, if one exists.

Hierarchy

Index

Constructors

constructor

Properties

Protected _children

_children: XmlNode[]

Accessors

parent

  • get parent(): XmlNode | undefined
  • Gets the parent of this node.

    Returns XmlNode | undefined

    The parent of this node.

Methods

children

  • Gets this node's children.

    Throws an exception if this node cannot have any children. Consult the appropriate subclass documentation for more details.

    Returns XmlNode[]

    This node's children.

comment

  • comment(content: string, index?: number): XmlComment
  • Inserts a new comment at the specified index. If no index is specified, the node is inserted at the end of this node's children.

    Parameters

    • content: string

      The data of the comment.

    • Default value index: number = this._children.length

      The index at which the node should be inserted. If no index is specified, the node is inserted at the end of this node's children.

    Returns XmlComment

    The newly created element.

decl

  • Inserts a new XML declaration at the beginning of this node's children.

    Parameters

    Returns XmlDecl

    The newly created XML declaration.

dtd

  • dtd(name: string, sysId?: string, pubId?: string, index?: number): XmlDtd
  • Inserts a new XML document type definition. Unless a different index is specified, the node is inserted immediately after the XML declaration if one exists, or at the beginning of this node's children if one does not.

    Parameters

    • name: string

      The name of the DTD.

    • Optional sysId: string

      The system identifier of the DTD, excluding quotation marks.

    • Optional pubId: string

      The public identifier of the DTD, excluding quotation marks. If a public identifier is provided, a system identifier must be provided as well.

    • Optional index: number

      The index at which the node should be inserted. If no index is specified, the node is inserted immediately after the XML declaration if one exists, or at the beginning of this node's children if one does not.

    Returns XmlDtd

    The newly created XML document type definition.

insertChild

  • Inserts the specified node into this node's children at the specified index. The node is not inserted if it is already present. If this node already has a parent, it is removed from that parent.

    Only XmlComment, XmlDecl, XmlDtd, or XmlProcInst nodes can be inserted. Furthermore, XmlDecl and XmlDtd nodes must be inserted in that order and must precede the XmlElement node. In addition, XmlComment or XmlProcInst nodes must follow the XmlDecl node.

    Parameters

    • node: XmlNode

      The node to insert.

    • Default value index: number = this._children.length

      The index at which to insert the node. Nodes at or after the index are shifted to the right. If no index is specified, the node is inserted at the end.

    Returns XmlNode | undefined

    The node inserted into this node's children, or undefined if no node was inserted.

next

  • Gets the node that follows this one, or undefined if no such node exists or if this node has no parent.

    Returns XmlNode | undefined

    The node that follows this one, or undefined if no such node exists or if this node has no parent.

prev

  • Gets the node that is previous to this one, or undefined if no such node exists or if this node has no parent.

    Returns XmlNode | undefined

    The node that is previous to this one, or undefined if no such node exists or if this node has no parent.

procInst

  • procInst(target: string, content?: string, index?: number): XmlProcInst
  • Inserts a new processing instruction at the specified index. If no index is specified, the node is inserted at the end of this node's children.

    Parameters

    • target: string

      The target of the processing instruction.

    • Optional content: string

      The data of the processing instruction, or undefined if there is no target.

    • Default value index: number = this._children.length

      The index at which the node should be inserted. If no index is specified, the node is inserted at the end of this node's children.

    Returns XmlProcInst

    The newly created processing instruction.

remove

  • Removes this node from its parent if this node has a parent.

    Returns XmlNode | undefined

    This node's parent, or undefined if it has no parent.

removeChild

  • removeChild(node: XmlNode): boolean
  • Removes the specified node from this node's children.

    Note that XmlElement nodes cannot be removed from this node; attempts to do so will result in an exception being thrown.

    Parameters

    • node: XmlNode

      The node to remove.

    Returns boolean

    Whether a node was removed.

removeChildAtIndex

  • removeChildAtIndex(index: number): XmlNode
  • Removes the node at the specified index from this node's children.

    Note that XmlElement nodes cannot be removed from this node; attempts to do so will result in an exception being thrown.

    Parameters

    • index: number

      The index at which the node to be removed is located.

    Returns XmlNode

    The node that was removed, or undefined if no node was removed.

root

  • Returns the root element of this document.

    Returns XmlElement

    The root element of this document.

toString

  • Returns an XML string representation of this node.

    Parameters

    Returns string

    An XML string representation of this node.

top

  • Returns the root node of the current hierarchy. If this node has no parent, this node itself is returned.

    Returns XmlNode

    The root node of the current hierarchy.

up

  • Gets the parent of this node.

    Returns XmlNode | undefined

Generated using TypeDoc