Element

public final class Element: ParentNode

By far the vast majority of objects (apart from text) that authors encounter when traversing a document are Element nodes. Assume the following XML document:

<elementExample id="demo">
  <subelement1/>
  <subelement2><subsubelement/></subelement2>
</elementExample>

When represented using the DOM, the top node is an Element node for elementExample, which contains two child Element nodes, one for subelement1 and one for subelement2. subelement1 contains no child nodes.

Elements may have attributes associated with them; since the Element class implements the Node protocol, the attributes property of the Node protocol may be used to retrieve a dictionary of the attributes for an element.

  • The nodeType of an Element is .element.

    Declaration

    Swift

    public static let nodeType: NodeType = .element
  • The nodeName of an Element is its tagName.

    Declaration

    Swift

    public final var nodeName: String
  • The nodeValue of an Element is nil.

    Declaration

    Swift

    public final let nodeValue: String? = nil
  • The children of the element.

    Declaration

    Swift

    public final var children: [Node]
  • The name of the element. For example, in:

    <elementExample id="demo">
       ...
    </elementExample>
    

    tagName has the value "elementExample".

    Declaration

    Swift

    public final var tagName: String
  • A dictionary that contains any attributes associated with the element. Keys are the names of attributes, and values are attribute values.

    Declaration

    Swift

    public final var attributes: [String : String]?
  • If an element has only a single child, and that child is a text node, return that text node’s value. Otherwise, return nil. It is recommended first to normalize the document or this element (see Node.normalize()).

    This method will return the text as it is represented in the single child text node. Whitespace will not be stripped or normalized (see String.trimmed and String.normalized).

    Declaration

    Swift

    public final var textValue: String?
  • Creates a new Element node.

    Declaration

    Swift

    public init(tagName: String, attributes: [String : String] = [:], children: [Node] = [])

    Parameters

    tagName

    A string that is the name of an element (in its start tag).

    attributes

    A dictionary that contains any attributes associated with the element. Keys are the names of attributes, and values are attribute values.

  • An element node first calls beginVisit(self) on the visitor. It then calls accept on each of the nodes in the children array, passing the visitor object. Finally, it calls endVisit(self) on the visitor.

    Declaration

    Swift

    public func accept(_ visitor: Visitor)