Each XML node created by libxml2 has: if it is an element (as determined by xmlIsElement), an optional element name, which is a string and is obtained with xmlGetName; if it is text (as determined by xmlIsText), an optional content string, obtained with xmlGetContent; a linked list of attributes of type LibxmlAttribute; a linked list of children (which are XML nodes), obtained with xmlFirstChild; and a pointer its next sibling, obtained with xmlGetNext.
XML nodes are mutable.
Internally, a pointer to the XML document containing the node accompanies the node.
i1 : n = xmlParse ///<foo> aabc <bar id="foo" name="too"> asdf </bar><coo/><coo>hi</coo><coo a="b">hi</coo></foo>///
o1 = <foo> aabc <bar id="foo" name="too"> asdf </bar><coo/><coo>hi</coo><coo a="b">hi</coo></foo>
o1 : LibxmlNode (xmlTypeDescription)
|
i2 : xmlIsElement n, xmlIsText n
o2 = (true, false)
o2 : Sequence
|
Since it is an element, we may use
xmlGetName to get its name.
i3 : xmlGetName n
o3 = foo
|
We use
xmlFirstChild to get the first node in the linked list of children, which happens to be text:
i4 : c = xmlFirstChild n
o4 = " aabc "
o4 : LibxmlNode (xmlTypeDescription)
|
i5 : xmlIsElement c, xmlIsText c
o5 = (false, true)
o5 : Sequence
|
We may follow the linked list of children of n.
i6 : c
o6 = " aabc "
o6 : LibxmlNode (xmlTypeDescription)
|
i7 : bar = xmlGetNext oo
o7 = <bar id="foo" name="too"> asdf </bar>
o7 : LibxmlNode (xmlTypeDescription)
|
i8 : xmlGetNext oo
o8 = <coo/>
o8 : LibxmlNode (xmlTypeDescription)
|
i9 : xmlGetNext oo
o9 = <coo>hi</coo>
o9 : LibxmlNode (xmlTypeDescription)
|
i10 : xmlGetNext oo
o10 = <coo a="b">hi</coo>
o10 : LibxmlNode (xmlTypeDescription)
|
i11 : xmlGetNext oo
|
Let’s examine the attributes of bar.
i12 : xmlFirstAttribute bar
o12 = id = "foo"
o12 : LibxmlAttribute
|
i13 : a = xmlGetNext oo
o13 = name = "too"
o13 : LibxmlAttribute
|
i14 : xmlGetNext oo
|
We may disassemble an attribute as follows.
i15 : xmlGetName a
o15 = name
|
i16 : b = xmlFirstChild a
o16 = "too"
o16 : LibxmlNode (xmlTypeDescription)
|
i17 : xmlGetNext oo
|
i18 : xmlIsText b
o18 = true
|
i19 : toString b
o19 = too
|
There are other functions that retrieve the entire list of attributes or children.
i20 : getChildren n
o20 = {" aabc " }
{<bar id="foo" name="too"> asdf </bar>}
{<coo/> }
{<coo>hi</coo> }
{<coo a="b">hi</coo> }
o20 : VerticalList
|
i21 : class \ oo
o21 = {LibxmlNode}
{LibxmlNode}
{LibxmlNode}
{LibxmlNode}
{LibxmlNode}
o21 : VerticalList
|
i22 : getAttributes bar
o22 = {id = "foo" }
{name = "too"}
o22 : VerticalList
|
i23 : class \ oo
o23 = {LibxmlAttribute}
{LibxmlAttribute}
o23 : VerticalList
|