Class XMLNode
This class represents an XML DOM structure.
Syntax
Constructors
XMLNode(String)
Pass a string to parse and create a new object.
Declaration
XMLNode XMLNode(String value)
Parameters
Type | Name | Description |
---|---|---|
String | value | A string to parse. |
Returns
Type | Description |
---|---|
XMLNode |
XMLNode(XMLNode)
Pass an object to copy.
Declaration
XMLNode XMLNode(XMLNode value)
Parameters
Type | Name | Description |
---|---|---|
XMLNode | value | XMLNode object. |
Returns
Type | Description |
---|---|
XMLNode |
Methods
addChild(XMLNode)
Add one node as a child node of the current node.
Declaration
Void addChild(XMLNode node)
Parameters
Type | Name | Description |
---|---|---|
XMLNode | node | The node to be added. |
Returns
Type | Description |
---|---|
Void |
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);
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. |
getName()
Gets the name of the XML tag.
Declaration
String getName()
Returns
Type | Description |
---|---|
String | The name of the XML tag. |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getName());
getParameter(String)
Gets a parameter (attribute) from the node with a given name.
Declaration
String getParameter(String name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The key to get the value for. |
Returns
Type | Description |
---|---|
String |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getParameter("type"));
getText()
Gets the text between two tags. For example, >TAG>Returns this text>/TAG>
Declaration
String getText()
Returns
Type | Description |
---|---|
String |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
xml.setText("Example text");
print(xml.getText());
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)
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. |
Remarks
If your path does not lead to a node, you will get a String with a null value back (can be checked with isNull()).
Examples
Given the following structure, you can get "Peter" by asking for "persons.1.name".
{ persons: [{name: "John"},{name: "Peter"}]}
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)
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. |
Remarks
If your path does not lead to a node, you will get an empty XMLnode back.
Examples
Given the following structure, you can get the node containing "Peter" by asking for "persons.1.name".
{ persons: [{name: "John"},{name: "Peter"}]}
setChildren(XMLNode[])
Sets an array of XMLNodes as the children of the current node.
Declaration
Void setChildren(XMLNode[] children)
Parameters
Type | Name | Description |
---|---|---|
XMLNode[] | children | Array of child nodes. |
Returns
Type | Description |
---|---|
Void |
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);
setName(String)
Sets the tag name of the node.
Declaration
Void setName(String name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The tag name of the node. |
Returns
Type | Description |
---|---|
Void |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
print(xml.getName());
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)
Parameters
Type | Name | Description |
---|---|---|
String | name | The parameter name. |
String | value | The parameter value. |
Returns
Type | Description |
---|---|
Void |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type1", "object1");
xml.setParameter("type", "object");
setText(String)
Sets the text between 2 tags.
Declaration
Void setText(String text)
Parameters
Type | Name | Description |
---|---|---|
String | text | The text between 2 tags. |
Returns
Type | Description |
---|---|
Void |
Examples
XMLNode xml = XMLNode("root");
xml.setName("Root");
xml.setParameter("type", "object");
xml.setText("Example text");
print(xml.getText());
toJSON(Integer)
Creates a string containing XML nodes formatted as a JSON document. Child nodes are also included.
Declaration
String toJSON(Integer indent)
Parameters
Type | Name | Description |
---|---|---|
Integer | indent | The text with a given number of spaces. |
Returns
Type | Description |
---|---|
String | XMLNodes converted to JSON. |
Remarks
Valid types: object, array, string, number, bool, and null.
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 }
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 |