Node

public protocol Node: Visitable

The Node protocol is the primary data type for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node protocol expose functionality related to children, not all objects implementing the Node protocol may have children. To address this distinction, there are two additional protocols implemented by node types:

  • ParentNode provides a getter and setter on the children property
  • LeafNode provides a getter on the children property that always returns an empty array.

This is a departure from the standard DOM which would throw an error when attempting to modify the children array of a leaf node.

The attributes nodeName, nodeValue, and attributes are included as a mechanism to get at node information without casting down to the specific derived type. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns nil.

The values of nodeName, nodeValue, and attributes vary according to the node type.

  • evaluate(path:) Extension method

    Selects nodes based on a path relative to this node. For example, in the following document:

    <a id="1">
      <b id="2">
        <c id="3"/>
      </b>
      <c id="4">
        <d id="5"/>
      </c>
    </a>
    

    evaluating the path ["a", "b", "c"] relative to the document would select the <c> element with id="3" but not the <c> element with id="4".

    In this example, starting at the document object (the parent of the root <a> element), select all children with nodeName == "a". From that set of nodes (with nodeName == "a"), select all children with nodeName == "b". Finally, from that set of nodes (with nodeName == "b" that are children of nodes with nodeName == "a"), select all children with nodeName == "c".

    Declaration

    Swift

    public final func evaluate(path: [String]) -> [Node]

    Parameters

    path

    An array of strings, each representing a nodeName in the path.

    Return Value

    An array of nodes corresponding to the specified path, relative to this node.

  • prettyPrint(indentWith:) Extension method

    Generates a formatted XML string representation of this node and its descendants.

    Declaration

    Swift

    public func prettyPrint(indentWith: String = "\t") -> String?

    Parameters

    indentWith

    The string used to indent the formatted string.

    Return Value

    A formatted XML string representation of this node and its descendants.

  • dump() Extension method

    Generates an unformatted XML string representation of this node and its descendants.

    Declaration

    Swift

    public func dump() -> String?

    Return Value

    A formatted XML string representation of this node and its descendants.

  • children(ofType:) Extension method

    Filters the children array, keeping only nodes of the specified type. Casts the nodes in the resulting array to the specified type.

    Declaration

    Swift

    public final func children<T: Node>(ofType type: T.Type) -> [T]

    Parameters

    type

    Include children of this type in the resulting array

    Return Value

    The nodes in the children array of the specified type

  • hasChildren Extension method

    A Boolean value indicating whether the children array is not empty.

    Declaration

    Swift

    public final var hasChildren: Bool
  • firstChild Extension method

    The first node in the children array.

    Declaration

    Swift

    public final var firstChild: Node?
  • lastChild Extension method

    The last node in the children array.

    Declaration

    Swift

    public final var lastChild: Node?
  • children(withName:) Extension method

    Returns an array of children with the given nodeName.

    Declaration

    Swift

    public final func children(withName name: String) -> [Node]

    Parameters

    name

    The node name to find.

    Return Value

    The children with the given node name.

  • childElements Extension method

    Returns the Element objects from the children array.

    Declaration

    Swift

    public final var childElements: [Element]
  • hasChildElements Extension method

    A Boolean value indicating whether the childElements array is not empty

    Declaration

    Swift

    public final var hasChildElements: Bool
  • firstChildElement Extension method

    The first element in the childElements array.

    Declaration

    Swift

    public final var firstChildElement: Element?
  • lastChildElement Extension method

    The last element in the childElements array.

    Declaration

    Swift

    public final var lastChildElement: Element?
  • childElements(withName:) Extension method

    Returns an array of child Element objects with the given nodeName.

    Declaration

    Swift

    public final func childElements(withName name: String) -> [Element]

    Parameters

    name

    The node name to find.

    Return Value

    The child elements with the given node name.