XMLNode

public final class XMLNode: XMLElement

The basic unit of an XML document — the default implementation of the protocol Node

  • Returns a value of a specified attribute of self.

    In the following example let’s assume that foo is of type XMLNode and represents the foo tag. In this case the value of foo["class"] is "top-header".

    <foo class="top-header">Hello, World!</foo>
    

    Attribute value can also be set. If initially there were no attibute with the specified name, it will be created, otherwise its value will be rewritten. For example, if we use foo["class"] = "subheader", then we get the following:

    <foo class="subheader">Hello, World!</foo>
    

    If the value we set is nil, the attribute will be removed:

    <foo>Hello, World!</foo>
    

    Complexity

    O(n), where n is the number of attributes.

    Declaration

    Swift

    public subscript(attributeName: String) -> String?

    Parameters

    attribute

    The name of an attribute.

    Return Value

    A value of a specified attribute of self, or nil if no such attribute exist.

  • Text content of the node. May be nil if no content is available.

    Declaration

    Swift

    public var text: String?
  • HTML content of the node. It is different from xml property, because the value can be formatted. May be nil if no content is available.

    Declaration

    Swift

    public var html: String?
  • xml

    XML content of the node, i. e. content as it has been loaded. May be nil if no content is available.

    Declaration

    Swift

    public var xml: String?
  • HTML content of the node without outermost tags. Only available if the html property is not nil.

    Declaration

    Swift

    public var innerHTML: String?
  • Value of the attribute class of the node. This property is nil if the node does not have class attribute

    Declaration

    Swift

    public var className: String?
  • Name of the corresponding tag for this node.

    Note

    Setting this property to nil does not make any change.

    Declaration

    Swift

    public var tagName: String?
  • Content of the node. May be nil if no content is available.

    Declaration

    Swift

    public var content: String?
  • Parent node of self in the DOM.

    In the following example foo is a parent for nodes bar and baz:

    <foo>
      <bar>Hello</bar>
      <baz>World</baz>
    </foo>
    

    If we now set baz to be a parent of bar, then we get the following:

    <foo>
      <baz>
        World
        <bar>Hello</bar>
      </baz>
    </foo>
    

    Declaration

    Swift

    public var parent: XMLElement?
  • Adds a new node as the previous sibling of self. If the new node was already inserted in a document, it is first unlinked from its existing context.

    Suppose we have the following:

    <foo>
      <baz>
        World
        <bar>Hello</bar>
      </baz>
    </foo>
    

    Let self represents the tag baz and bar represents the tag bar. After calling addPreviousSibling(bar) on self, here will be the result:

    <foo>
      <bar>Hello</bar>
      <baz>World</baz>
    </foo>
    

    Declaration

    Swift

    public func addPreviousSibling(_ node: XMLElement)

    Parameters

    node

    A node to add as a previous sibling of self.

  • Adds a new node as the next sibling of self. If the new node was already inserted in a document, it is first unlinked from its existing context.

    Suppose we have the following:

    <foo>
      <baz>
        Hello
        <bar>World</bar>
      </baz>
    </foo>
    

    Let self represents the tag baz and bar represents the tag bar. After calling addNextSibling(bar) on self, here will be the result:

    <foo>
      <baz>Hello</baz>
      <bar>World</bar>
    </foo>
    

    Declaration

    Swift

    public func addNextSibling(_ node: XMLElement)

    Parameters

    node

    A node to add as a next sibling of self.

  • Removes a child node of self.

    In the example below let foo tag is representet by self, whereas bar tag is represented by bar.

    <foo>
      Hello
      <bar>World</bar>
    </foo>
    

    Calling foo.removeChild(bar) results in the following: xml <foo> Hello </foo>

    Declaration

    Swift

    public func removeChild(_ node: XMLElement)

    Parameters

    node

    A node to remove. self must be the parent of the node.

  • Searches for a node from a current node by provided XPath.

    Declaration

    Swift

    public func search(byXPath xpath: String, namespaces: [String : String]?) -> XPathResult

    Parameters

    xpath

    XPath to search by.

    namespaces

    XML namespace to search in. Default value is nil.

    Return Value

    XPath enum case with an associated value.

  • Searches for a node from a current node by provided CSS selector.

    Declaration

    Swift

    public func search(byCSSSelector selector: String, namespaces: [String : String]?) -> XPathResult

    Parameters

    selector

    CSS selector to search by.

    namespaces

    XML namespace to search in. Default value is nil.

    Return Value

    XPath enum case with an associated value.