XMLElement

public protocol XMLElement: Node

Instances of conforming types can provide a number of means to see and edit an internal structure of XML nodes.

  • Parent element of self in the DOM.

    In the following example foo is a parent for elements 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

    var parent: XMLElement?
  • Returns a value of a specified attribute of self.

    In the following example let’s assume that foo is of type XMLElement 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

    subscript(attribute: String) -> String? { get set }

    Parameters

    attribute

    The name of an attribute.

    Return Value

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

  • 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

    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

    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 removeChild(bar) results in the following:

    <foo>
      Hello
    </foo>
    

    Declaration

    Swift

    func removeChild(_ node: XMLElement)

    Parameters

    node

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