v2026.5.3
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.

XmlParser

Parses an XML document

input := "";
input += "<inventory title=\"OmniCorp Store #45x10^3\">";
input += "<section name=\"health\">";
input += "<item upc=\"123456789\" stock=\"12\">";
input += "<name>Invisibility Cream</name>";
input += "<price>14.50</price>";
input += "<description>Makes you invisible</description>";
input += "</item>";
input += "<item upc=\"445322344\" stock=\"18\">";
input += "<name>Levitation Salve</name>";
input += "<price>23.99</price>";
input += "<description>Levitate yourself for up to 3 hours per application</description>";
input += "</item>";
input += "</section>";
input += "<section name=\"food\">";
input += "<item upc=\"485672034\" stock=\"653\">";
input += "<name>Blork and Freen Instameal</name>";
input += "<price>4.95</price>";
input += "<description>A tasty meal input a tablet; just add water</description>";
input += "</item>";
input += "<item upc=\"132957764\" stock=\"44\">";
input += "<name>Grob winglets</name>";
input += "<price>3.56</price>";
input += "<description>Tender winglets of Grob. Just add water</description>";
input += "</item>";
input += "</section>";
input += "</inventory>";

parser := XmlParser->New(input);
if(parser->Parse()) {
   # get first item
   results := parser->FindElements("/inventory/section[1]/item[1]")<XmlElement>;
   if(results <> Nil) {
      Console->Print("items: ")->PrintLine(results->Size());
   };
   # get all prices
   results := parser->FindElements("/inventory/section/item/price")<XmlElement>;
   if(results <> Nil) {
      each(i : results) {               
         element := results->Get(i)->As(XmlElement);
         element->GetContent()->PrintLine();
      };
   };
   # get names
   results := parser->FindElements("/inventory/section/item/name")<XmlElement>;
   if(results <> Nil) {
      Console->Print("names: ")->PrintLine(results->Size());
   };
};

Operations

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("<store><item><price>9.99</price></item></store>");
if(parser->Parse()) {
  results := parser->FindElements("/store/item/price")<XmlElement>;
  each(e := results) {
    e->GetContent()->PrintLine();
  };
};

GetEncoding #

Gets the XML encoding

method : public : GetEncoding() ~ String

Return

TypeDescription
StringXML encoding

Example

parser := XmlParser->New("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>");
if(parser->Parse()) {
  parser->GetEncoding()->PrintLine();
};

GetError #

Get the current parsing error

method : public : GetError() ~ String

Return

TypeDescription
Stringcurrent parsing error

Example

parser := XmlParser->New("<unclosed>");
if(parser->Parse() = false) {
  parser->GetError()->PrintLine();
};

GetRoot #

Gets the root XML element

method : public : GetRoot() ~ XmlElement

Return

TypeDescription
XmlElementroot XML element

Example

parser := XmlParser->New("<library><book>Dune</book></library>");
if(parser->Parse()) {
  root := parser->GetRoot();
  root->GetName()->PrintLine();
};

GetVersion #

Gets XML version

method : public : GetVersion() ~ String

Return

TypeDescription
StringXML version

Example

parser := XmlParser->New("<?xml version=\"1.0\"?><root/>");
if(parser->Parse()) {
  parser->GetVersion()->PrintLine();
};

New # constructor

Default constructor

New(string:String)

Parameters

NameTypeDescription
stringStringXML to parse

Example

parser := XmlParser->New("<root><item>hello</item></root>");
if(parser->Parse()) {
  parser->GetRoot()->GetName()->PrintLine();
};

New # constructor

Default constructor

New(buffer:Char[])

Parameters

NameTypeDescription
bufferCharXML to parse

Example

xml := "<data><val>42</val></data>";
parser := XmlParser->New(xml->ToCharArray());
if(parser->Parse()) {
  parser->GetRoot()->GetName()->PrintLine();
};

Parse #

Parses the XML document

method : public : Parse() ~ Bool

Return

TypeDescription
Booltrue if parsed, false otherwise

Example

parser := XmlParser->New("<root><item>one</item><item>two</item></root>");
if(parser->Parse()) {
  items := parser->FindElements("/root/item")<XmlElement>;
  Console->PrintLine(items->Size());
};

TextToElement # function

Parses an XmlElement text

function : TextToElement(text:String) ~ XmlElement

Parameters

NameTypeDescription
textStringJSON text

Return

TypeDescription
XmlElementroot document element

Example

elem := XmlParser->TextToElement("<msg><body>Hello</body></msg>");
if(elem <> Nil) {
  elem->GetFirstChild("body")->GetContent()->PrintLine();
};

ToString #

Produces an XML string representation of the document

method : public : ToString() ~ String

Return

TypeDescription
Stringstring representation of the document

Example

parser := XmlParser->New("<root><child>text</child></root>");
if(parser->Parse()) {
  parser->ToString()->PrintLine();
};