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
attributeofself.In the following example let’s assume that
foois of typeXMLNodeand represents thefoo
tag. In this case the value offoo["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
attributeThe name of an attribute.
Return Value
A value of a specified
attributeofself, ornilif no such attribute exist.
-
Text content of the node. May be
nilif no content is available.Declaration
Swift
public var text: String? -
HTML content of the node. It is different from
xmlproperty, because the value can be formatted. May benilif no content is available.Declaration
Swift
public var html: String? -
XML content of the node, i. e. content as it has been loaded. May be
nilif no content is available.Declaration
Swift
public var xml: String? -
HTML content of the node without outermost tags. Only available if the
htmlproperty is notnil.Declaration
Swift
public var innerHTML: String? -
Value of the attribute
class
of the node. This property isnilif the node does not haveclass
attributeDeclaration
Swift
public var className: String? -
Name of the corresponding tag for this node.
Note
Setting this property tonildoes not make any change.Declaration
Swift
public var tagName: String? -
Content of the node. May be
nilif no content is available.Declaration
Swift
public var content: String?
-
Parent node of
selfin the DOM.In the following example
foo
is a parent for nodesbar
andbaz
:<foo> <bar>Hello</bar> <baz>World</baz> </foo>If we now set
baz
to be a parent ofbar
, then we get the following:<foo> <baz> World <bar>Hello</bar> </baz> </foo>Declaration
Swift
public var parent: XMLElement? -
Adds a new
nodeas the previous sibling ofself. 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
selfrepresents the tagbaz
andbarrepresents the tagbar
. After callingaddPreviousSibling(bar)onself, here will be the result:<foo> <bar>Hello</bar> <baz>World</baz> </foo>Declaration
Swift
public func addPreviousSibling(_ node: XMLElement)Parameters
nodeA node to add as a previous sibling of
self. -
Adds a new
nodeas the next sibling ofself. 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
selfrepresents the tagbaz
andbarrepresents the tagbar
. After callingaddNextSibling(bar)onself, here will be the result:<foo> <baz>Hello</baz> <bar>World</bar> </foo>Declaration
Swift
public func addNextSibling(_ node: XMLElement)Parameters
nodeA node to add as a next sibling of
self. -
Removes a child node of
self.In the example below let
foo
tag is representet byself, whereasbar
tag is represented bybar.<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
nodeA node to remove.
selfmust be the parent of thenode.
-
Searches for a node from a current node by provided XPath.
Declaration
Swift
public func search(byXPath xpath: String, namespaces: [String : String]?) -> XPathResultParameters
xpathXPath to search by.
namespacesXML namespace to search in. Default value is
nil.Return Value
XPathenum 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]?) -> XPathResultParameters
selectorCSS selector to search by.
namespacesXML namespace to search in. Default value is
nil.Return Value
XPathenum case with an associated value.
XMLNode Class Reference