Class XMLNode
This class represents an XML DOM structure.
Constructors
Methods
toString(Integer)
Creates a string containing the XMLNode as a formatted string. Child nodes are also included.
Declaration
String toString(Integer indent)
Parameters
Type | Name | Description |
Integer | indent | The text with a given number of spaces. |
Returns
Type | Description |
String |
toJSON(Integer)
Creates a string containing XML nodes formatted as a JSON document. Child nodes are also included.
Declaration
String toJSON(Integer indent)
Examples
XMLNode xml = XMLNode("root");
xMenu.setParameter("type", "object");
XMLNode xMenu = XMLNode("menu");
xMenu.setParameter("type", "string");
xMenu.setText("truls");
xml.addChild(xMenu);
XMLNode xFoo = XMLNode("foo");
xFoo.setParameter("type", "number");
xFoo.setText("1.23456");
xml.addChild(xFoo);
print(xml.toString(0));
Outputs: { "menu": "truls", "foo": 1.23456 }
Parameters
Type | Name | Description |
Integer | indent | The text with a given number of spaces. |
Returns
Type | Description |
String | XMLNodes converted to JSON. |
addChild(XMLNode)
Add one node as a child node of the current node.
Declaration
Void addChild(XMLNode node)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
xml.setText("Example text");
XMLNode xMenu = XMLNode("menu");
xMenu.setParameter("type", "string");
xml.addChild(xMenu);
Parameters
Type | Name | Description |
XMLNode | node | The node to be added. |
Returns
Type | Description |
Void |
getChildren()
Returns an array of the current node's children.
Declaration
XMLNode[] getChildren()
Returns
Type | Description |
XMLNode[] | Returns an array of the current node's children. |
setChildren(XMLNode[])
Sets an array of XMLNodes as the children of the current node.
Declaration
Void setChildren(XMLNode[] children)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
XMLNode xMenu = XMLNode("menu");
xMenu.setParameter("type", "string");
xMenu.setText("truls");
XMLNode xFoo = XMLNode("foo");
xFoo.setParameter("type", "number");
xFoo.setText("1.23456");
XMLNode[2] array;
array[0] = xMenu;
array[1] = xFoo;
xml.setChildren(array);
Parameters
Type | Name | Description |
XMLNode[] | children | Array of child nodes. |
Returns
Type | Description |
Void |
getName()
Gets the name of the XML tag.
Declaration
String getName()
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getName());
Returns
Type | Description |
String | The name of the XML tag. |
setName(String)
Sets the tag name of the node.
Declaration
Void setName(String name)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getName());
Parameters
Type | Name | Description |
String | name | The tag name of the node. |
Returns
Type | Description |
Void |
getParameter(String)
Gets a parameter (attribute) from the node with a given name.
Declaration
String getParameter(String name)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getParameter("type"));
Parameters
Type | Name | Description |
String | name | The key to get the value for. |
Returns
Type | Description |
String |
setParameter(String, String)
Sets a parameter with name and value. A node can have any number of parameters but all names must be unique.
Declaration
Void setParameter(String name, String value)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type1", "object1");
xml.setParameter("type", "object");
Parameters
Type | Name | Description |
String | name | The parameter name. |
String | value | The parameter value. |
Returns
Type | Description |
Void |
getText()
Gets the text between two tags. For example, >TAG>Returns this text>/TAG>
Declaration
String getText()
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
xml.setText("Example text");
print(xml.getText());
Returns
Type | Description |
String |
setText(String)
Sets the text between 2 tags.
Declaration
Void setText(String text)
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
xml.setText("Example text");
print(xml.getText());
Parameters
Type | Name | Description |
String | text | The text between 2 tags. |
Returns
Type | Description |
Void |
getValueFromPath(String)
This function will return the value for the given path. The path should be a dot-separated string containing either names of nodes or indices.
Declaration
String getValueFromPath(String path)
Examples
Given the following structure, you can get "Peter" by asking for "persons.1.name".
{ persons: [{name: "John"},{name: "Peter"}]}
Parameters
Type | Name | Description |
String | path | A dot-separated string containing either names of nodes or indexes. |
Returns
Type | Description |
String | The value for the given path. |
getNodeFromPath(String)
This function will return the XMLNode for the given path. The path should be a dot-separated string containing either names of nodes or indices.
Declaration
XMLNode getNodeFromPath(String path)
Examples
Given the following structure, you can get the node containing "Peter" by asking for "persons.1.name".
{ persons: [{name: "John"},{name: "Peter"}]}
Parameters
Type | Name | Description |
String | path | A dot-separated string containing either names of nodes or indexes. |
Returns
Type | Description |
The Node for the given path. |