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
| Name | Type | Description |
|---|---|---|
| path | String | search string |
Return
| Type | Description |
|---|---|
| Vector<XmlElement> | matching XML elements |
GetEncoding
Gets the XML encoding
method : public : GetEncoding() ~ StringReturn
| Type | Description |
|---|---|
| String | XML encoding |
GetError
Get the current parsing error
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | current parsing error |
GetRoot
Gets the root XML element
method : public : GetRoot() ~ XmlElementReturn
| Type | Description |
|---|---|
| XmlElement | root XML element |
GetVersion
Gets XML version
method : public : GetVersion() ~ StringReturn
| Type | Description |
|---|---|
| String | XML version |
Parse
Parses the XML document
method : public : Parse() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if parsed, false otherwise |
TextToElement
Parses an XmlElement text
function : TextToElement(text:String) ~ XmlElementParameters
| Name | Type | Description |
|---|---|---|
| text | String | JSON text |
Return
| Type | Description |
|---|---|
| XmlElement | root document element |
ToString
Produces an XML string representation of the document
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation of the document |