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
ofself
.In the following example let’s assume that
foo
is of typeXMLNode
and 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
attribute
The name of an attribute.
Return Value
A value of a specified
attribute
ofself
, ornil
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 benil
if 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
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 notnil
.Declaration
Swift
public var innerHTML: String?
-
Value of the attribute
class
of the node. This property isnil
if the node does not haveclass
attributeDeclaration
Swift
public var className: String?
-
Name of the corresponding tag for this node.
Note
Setting this property tonil
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 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
node
as 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
self
represents the tagbaz
andbar
represents 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
node
A node to add as a previous sibling of
self
. -
Adds a new
node
as 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
self
represents the tagbaz
andbar
represents 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
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 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
node
A node to remove.
self
must 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]?) -> 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.