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 elementsbar
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
var parent: XMLElement?
-
Returns a value of a specified
attribute
ofself
.In the following example let’s assume that
foo
is of typeXMLElement
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
subscript(attribute: String) -> String? { get set }
Parameters
attribute
The name of an attribute.
Return Value
A value of a specified
attribute
ofself
, ornil
if no such attribute exist. -
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
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
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
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 thenode.