v2026.6.4
All Bundles
Bundle XML parsing and building library. XmlParser reads XML into an XmlElement tree with attribute and child-node navigation; XmlBuilder constructs XML documents. Compile with -lib xml.

XmlElement

Represents an XML element

Example

parser := XmlParser->New(xml_string);
if(parser->Parse()) {
   root := parser->GetRoot();
   root->GetName()->PrintLine();
   children := root->GetChildren()<XmlElement>;
   each(child := children) {
      child->GetContent()->PrintLine();
   };
};

Operations

AddAttribute #

Adds an element attribute

method : public : AddAttribute(attrib:XmlAttribute) ~ Nil

Parameters

NameTypeDescription
attribXmlAttributeelement attribute

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "img");
elem->AddAttribute(XmlAttribute->New("src", "logo.png"));
elem->GetAttribute("src")->GetValue()->PrintLine();

AddChild #

Adds an XML subelement tag

method : public : AddChild(tag:XmlElement) ~ Nil

Parameters

NameTypeDescription
tagXmlElementelement tag

Example

root := XmlElement->New(XmlElement->Type->ELEMENT, "list");
child := XmlElement->New(XmlElement->Type->ELEMENT, "entry");
child->SetContent("item one");
root->AddChild(child);
root->ToString()->PrintLine();

DecodeString # function

Decodes an XML string

function : DecodeString(input:String) ~ String

Parameters

NameTypeDescription
inputStringstring to decode

Return

TypeDescription
Stringdecoded string

Example

encoded := "&lt;b&gt;bold&lt;/b&gt;";
decoded := XmlElement->DecodeString(encoded);
decoded->PrintLine();

EncodeString # function

Encodes an XML string, including whitespace as numeric character references (for exact whitespace preservation in attribute values). For readable content encoding see EncodeText.

function : EncodeString(input:String) ~ String

Parameters

NameTypeDescription
inputStringstring to encode

Return

TypeDescription
Stringencoded string

Example

raw := "price < 10 & size > 5";
encoded := XmlElement->EncodeString(raw);
encoded->PrintLine();

EncodeText # function

Encodes the five XML markup characters (< > & " ') and leaves everything else — including whitespace — untouched. Use this for readable element content and attribute values; EncodeString additionally encodes whitespace as numeric references for exact whitespace preservation.

function : EncodeText(input:String) ~ String

Parameters

NameTypeDescription
inputStringplain text

Return

TypeDescription
Stringtext safe to embed in element content or attribute values

Example

raw := "price < 10 & size > 5";
XmlElement->EncodeText(raw)->PrintLine();   # price &lt; 10 &amp; size &gt; 5

FindElements #

Finds matching XML elements using xpath like syntax. Supports 'first()', 'last()' and '[cdata]' functions.

method : public : FindElements(path:String) ~ Vector<XmlElement>

Parameters

NameTypeDescription
pathStringsearch string

Return

TypeDescription
Vector<XmlElement>matching XML elements

Example

parser := XmlParser->New("<catalog><book><title>Dune</title></book></catalog>");
if(parser->Parse()) {
  results := parser->GetRoot()->FindElements("/catalog/book/title")<XmlElement>;
  results->Get(0)->GetContent()->PrintLine();
};

Get #

Gets a child element

method : public : Get(i:Int) ~ XmlElement

Parameters

NameTypeDescription
iIntchild index

Return

TypeDescription
XmlElementchild element, Nil if not found

Example

parser := XmlParser->New("<root><first/><second/></root>");
if(parser->Parse()) {
  parser->GetRoot()->Get(1)->GetName()->PrintLine();
};

GetAttribute #

Gets an element attribute

method : public : GetAttribute(key:String) ~ XmlAttribute

Parameters

NameTypeDescription
keyStringattribute name

Return

TypeDescription
XmlAttributeelement attribute

Example

parser := XmlParser->New("<link href=\"http://example.com\"/>");
if(parser->Parse()) {
  href := parser->GetRoot()->GetAttribute("href");
  href->GetValue()->PrintLine();
};

GetChildren # native

Get all child elements that matchs the given filter

method : public : native : GetChildren(name:String) ~ Vector<XmlElement>

Parameters

NameTypeDescription
nameStringelement name

Return

TypeDescription
Vector<XmlElement>child elements

Example

parser := XmlParser->New("<store><item>a</item><item>b</item><desc>info</desc></store>");
if(parser->Parse()) {
  items := parser->GetRoot()->GetChildren("item")<XmlElement>;
  Console->PrintLine(items->Size());
};

GetChildren # native

Get all child elements that matchs the given filter

method : public : native : GetChildren(name:String, attrib:String) ~ Vector<XmlElement>

Parameters

NameTypeDescription
nameStringelement name
attribStringattrib name

Return

TypeDescription
Vector<XmlElement>child elements

Example

parser := XmlParser->New("<root><item id=\"1\">a</item><item>b</item></root>");
if(parser->Parse()) {
  tagged := parser->GetRoot()->GetChildren("item", "id")<XmlElement>;
  Console->PrintLine(tagged->Size());
};

GetChildren # native

Get all child elements that matchs the given filter

method : public : native : GetChildren(name:String, max:Int) ~ Vector<XmlElement>

Parameters

NameTypeDescription
nameStringelement name
maxIntmax number of elements to return

Return

TypeDescription
Vector<XmlElement>child elements

Example

parser := XmlParser->New("<root><item>a</item><item>b</item><item>c</item></root>");
if(parser->Parse()) {
  first_two := parser->GetRoot()->GetChildren("item", 2)<XmlElement>;
  Console->PrintLine(first_two->Size());
};

GetChildren #

Get all children

method : public : GetChildren() ~ Vector<XmlElement>

Return

TypeDescription
Vector<XmlElement>child elements

Example

parser := XmlParser->New("<root><a/><b/><c/></root>");
if(parser->Parse()) {
  each(child := parser->GetRoot()->GetChildren()) {
    child->GetName()->PrintLine();
  };
};

GetContent #

Gets the element content as it appeared in the source: entity references such as &lt; are NOT decoded. Use GetDecodedContent for the decoded text.

method : public : GetContent() ~ String

Return

TypeDescription
Stringraw element content

Example

parser := XmlParser->New("<title>Hello World</title>");
if(parser->Parse()) {
  parser->GetRoot()->GetContent()->PrintLine();
};

GetDecodedContent #

Gets the element content with XML entity references decoded (&lt; &gt; &amp; &quot; &apos; and numeric references).

method : public : GetDecodedContent() ~ String

Return

TypeDescription
Stringdecoded element content

Example

parser := XmlParser->New("<title>5 &lt; 6</title>");
if(parser->Parse()) {
  parser->GetRoot()->GetDecodedContent()->PrintLine();   # 5 < 6
};

GetElementExpression #

~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # parses element expression # tags ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

method : private : GetElementExpression() ~ String

Parameters

NameTypeDescription

GetElementName #

~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # parses element name ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

method : private : GetElementName() ~ String

Parameters

NameTypeDescription

GetFirstChild # native

Get the first element that matchs the given filter

method : public : native : GetFirstChild(name:String) ~ XmlElement

Parameters

NameTypeDescription
nameStringelement name

Return

TypeDescription
XmlElementfirst match, Nil otherwise

Example

parser := XmlParser->New("<list><item>first</item><item>second</item></list>");
if(parser->Parse()) {
  parser->GetRoot()->GetFirstChild("item")->GetContent()->PrintLine();
};

GetLastChild # native

Get the last element that matchs the given filter

method : public : native : GetLastChild(name:String) ~ XmlElement

Parameters

NameTypeDescription
nameStringelement name

Return

TypeDescription
XmlElementlast match, Nil otherwise

Example

parser := XmlParser->New("<list><item>first</item><item>last</item></list>");
if(parser->Parse()) {
  parser->GetRoot()->GetLastChild("item")->GetContent()->PrintLine();
};

GetName #

Gets the element name

method : public : GetName() ~ String

Return

TypeDescription
Stringelement name

Example

parser := XmlParser->New("<product><sku>ABC-1</sku></product>");
if(parser->Parse()) {
  parser->GetRoot()->GetName()->PrintLine();
};

GetNamespace #

Gets the element namespace

method : public : GetNamespace() ~ String

Return

TypeDescription
Stringelement namespace

Example

parser := XmlParser->New("<svg:rect xmlns:svg=\"http://www.w3.org/2000/svg\"/>");
if(parser->Parse()) {
  parser->GetRoot()->GetNamespace()->PrintLine();
};

GetParent #

Gets a child's parent element

method : public : GetParent() ~ XmlElement

Return

TypeDescription
XmlElementparent element

Example

parser := XmlParser->New("<root><child/></root>");
if(parser->Parse()) {
  child := parser->GetRoot()->Get(0);
  child->GetParent()->GetName()->PrintLine();
};

GetType #

Gets the element type

method : public : GetType() ~ XmlElement->Type

Return

TypeDescription
XmlElement->Typeelement type

Example

parser := XmlParser->New("<root/>");
if(parser->Parse()) {
  t := parser->GetRoot()->GetType();
  if(t = XmlElement->Type->ELEMENT) {
    "is element"->PrintLine();
  };
};

New # constructor

Constructor

New(type:XmlElement->Type, name:String)

Parameters

NameTypeDescription
typeXmlElement->Typeelement type
nameStringelement name

New # constructor

Constructor

New(type:XmlElement->Type, name:String, content:String)

Parameters

NameTypeDescription
typeXmlElement->Typeelement type
nameStringelement name
contentStringcontent type

New # constructor

Constructor

New(type:XmlElement->Type, name:String, attribs:Hash<String,XmlAttribute>, content:String)

Parameters

NameTypeDescription
typeXmlElement->Typeelement type
nameStringelement name
attribsHash<String,XmlAttribute>element name/value pairs
contentStringcontent type

ProcessExpression #

~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # parses element expression # tags ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

method : private : ProcessExpression() ~ Vector<XmlElement>

Parameters

NameTypeDescription

SetContent #

Sets the element content AS-IS: the text is serialized verbatim, so markup characters must already be encoded (see SetEncodedContent and EncodeText).

method : public : SetContent(content:String) ~ Nil

Parameters

NameTypeDescription
contentStringraw element content

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "desc");
elem->SetContent("A widget");
elem->GetContent()->PrintLine();

SetEncodedContent #

Sets the element content, encoding XML markup characters so the serialized document stays well-formed.

method : public : SetEncodedContent(content:String) ~ Nil

Parameters

NameTypeDescription
contentStringplain text content (encoded automatically)

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "desc");
elem->SetEncodedContent("5 < 6 & 7");

SetName #

Sets the element name

method : public : SetName(name:String) ~ Nil

Parameters

NameTypeDescription
nameStringelement name

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "old");
elem->SetName("new");
elem->GetName()->PrintLine();

SetNamespace #

Sets the element namespace

method : public : SetNamespace(namespace:String) ~ Nil

Parameters

NameTypeDescription
namespaceStringelement namespace

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "rect");
elem->SetNamespace("svg");
elem->GetNamespace()->PrintLine();

SetParent #

Set a child's parent element

method : public : SetParent(parent:XmlElement) ~ Nil

Parameters

NameTypeDescription
parentXmlElementparent element

Example

parent := XmlElement->New(XmlElement->Type->ELEMENT, "parent");
child := XmlElement->New(XmlElement->Type->ELEMENT, "child");
child->SetParent(parent);
child->GetParent()->GetName()->PrintLine();

SetType #

Sets the element type

method : public : SetType(type:XmlElement->Type) ~ Nil

Parameters

NameTypeDescription
typeXmlElement->Typeelement type

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "item");
elem->SetType(XmlElement->Type->UNARY_ELEMENT);

Size #

Get the number of child elements

method : public : Size() ~ Int

Return

TypeDescription
Intnumber of child elements

Example

parser := XmlParser->New("<root><a/><b/><c/></root>");
if(parser->Parse()) {
  Console->PrintLine(parser->GetRoot()->Size());
};

ToString #

Creates a string representation of the element

method : public : ToString() ~ String

Return

TypeDescription
Stringstring representation of the element

Example

elem := XmlElement->New(XmlElement->Type->ELEMENT, "note");
elem->SetContent("Buy milk");
elem->ToString()->PrintLine();

ToString #

~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # serializes an element # into a string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

method : private : ToString() ~ Nil

Parameters

NameTypeDescription