All Bundles

XmlParser

Parses an XML document

Operations

Code example:

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());
   };
};

New

Default constructor

New(string:String)
Parameters
NameTypeDescription
stringStringXML to parse

Default constructor

New(buffer:Char[])
Parameters
NameTypeDescription
bufferChar[]XML to parse

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

GetEncoding

Gets the XML encoding

method : public : GetEncoding() ~ String
Return
TypeDescription
StringXML encoding

GetError

Get the current parsing error

method : public : GetError() ~ String
Return
TypeDescription
Stringcurrent parsing error

GetRoot

Gets the root XML element

method : public : GetRoot() ~ XmlElement
Return
TypeDescription
XmlElementroot XML element

GetVersion

Gets XML version

method : public : GetVersion() ~ String
Return
TypeDescription
StringXML version

Parse

Parses the XML document

method : public : Parse() ~ Bool
Return
TypeDescription
Booltrue if parsed, false otherwise

TextToElement

Parses an XmlElement text

function : TextToElement(text:String) ~ XmlElement
Parameters
NameTypeDescription
textStringJSON text

Return
TypeDescription
XmlElementroot document element

ToString

Produces an XML string representation of the document

method : public : ToString() ~ String
Return
TypeDescription
Stringstring representation of the document