XmlElement
Represents an XML element
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
- New
- AddAttribute
- AddChild
- DecodeString
- EncodeString
- FindElements
- Get
- GetAttribute
- GetChildren
- GetContent
- GetFirstChild
- GetLastChild
- GetName
- GetNamespace
- GetParent
- GetType
- SetContent
- SetName
- SetNamespace
- SetParent
- SetType
- Size
- ToString
AddAttribute #
Adds an element attribute
method : public : AddAttribute(attrib:XmlAttribute) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| attrib | XmlAttribute | element 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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| tag | XmlElement | element 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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| input | String | string to decode |
Return
| Type | Description |
|---|---|
| String | decoded string |
Example
encoded := "<b>bold</b>";
decoded := XmlElement->DecodeString(encoded);
decoded->PrintLine();EncodeString # function
Encodes an XML string
function : EncodeString(input:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| input | String | string to encode |
Return
| Type | Description |
|---|---|
| String | encoded string |
Example
raw := "price < 10 & size > 5";
encoded := XmlElement->EncodeString(raw);
encoded->PrintLine();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 |
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) ~ XmlElementParameters
| Name | Type | Description |
|---|---|---|
| i | Int | child index |
Return
| Type | Description |
|---|---|
| XmlElement | child 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) ~ XmlAttributeParameters
| Name | Type | Description |
|---|---|---|
| key | String | attribute name |
Return
| Type | Description |
|---|---|
| XmlAttribute | element 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
| Name | Type | Description |
|---|---|---|
| name | String | element name |
Return
| Type | Description |
|---|---|
| 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
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| attrib | String | attrib name |
Return
| Type | Description |
|---|---|
| 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
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| max | Int | max number of elements to return |
Return
| Type | Description |
|---|---|
| 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
| Type | Description |
|---|---|
| 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
method : public : GetContent() ~ StringReturn
| Type | Description |
|---|---|
| String | element content |
Example
parser := XmlParser->New("<title>Hello World</title>");
if(parser->Parse()) {
parser->GetRoot()->GetContent()->PrintLine();
};GetFirstChild # native
Get the first element that matchs the given filter
method : public : native : GetFirstChild(name:String) ~ XmlElementParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
Return
| Type | Description |
|---|---|
| XmlElement | first 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) ~ XmlElementParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
Return
| Type | Description |
|---|---|
| XmlElement | last 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() ~ StringReturn
| Type | Description |
|---|---|
| String | element 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() ~ StringReturn
| Type | Description |
|---|---|
| String | element 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() ~ XmlElementReturn
| Type | Description |
|---|---|
| XmlElement | parent 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->TypeReturn
| Type | Description |
|---|---|
| XmlElement->Type | element 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
| Name | Type | Description |
|---|---|---|
| type | XmlElement->Type | element type |
| name | String | element name |
New # constructor
Constructor
New(type:XmlElement->Type, name:String, content:String)Parameters
| Name | Type | Description |
|---|---|---|
| type | XmlElement->Type | element type |
| name | String | element name |
| content | String | content type |
New # constructor
Constructor
New(type:XmlElement->Type, name:String, attribs:Hash<String,XmlAttribute>, content:String)Parameters
| Name | Type | Description |
|---|---|---|
| type | XmlElement->Type | element type |
| name | String | element name |
| attribs | Hash<String,XmlAttribute> | element name/value pairs |
| content | String | content type |
SetContent #
Sets the element content
method : public : SetContent(content:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| content | String | element content |
Example
elem := XmlElement->New(XmlElement->Type->ELEMENT, "desc");
elem->SetContent("A widget");
elem->GetContent()->PrintLine();SetName #
Sets the element name
method : public : SetName(name:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| name | String | element 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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| namespace | String | element 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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| parent | XmlElement | parent 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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| type | XmlElement->Type | element type |
Example
elem := XmlElement->New(XmlElement->Type->ELEMENT, "item");
elem->SetType(XmlElement->Type->UNARY_ELEMENT);