XML Javascript library

The XML class provides means to load, parse, extract and manipulate XML data or BML data (Binary XML).
It shares a lot of common feature with the JSON parser.

Warning:
indexes start at 1 and not 0 to be more compliant to XPATH.

The parsed tree (DOM) can be manipulated (changing XML attributes value, or content of text nodes) and persisted in RMS for later use.

When the player is compiled with the BML (Binary XML) parser, the XML.open() methods can parse BML streams by passing the XML.BML_ENCODING parameter.


1.4.1

Noteworthy:

New methods: XML.open() with callback support, XML.setAttrValue(), XML.setValue(), XML.setChildValue(), XML.save(), XML.serialize().

New constants: XML.BML_ENCODING.

Deleted constants: XML.KEEP_ALIVE.


int open (String buffer,int mode):
Read data contained in buffer, build an internal tree (DOM) and return an identifier that must be used for all subsequent operations on this DOM. The meaning of buffer is given by mode.
If XML.URL is used, then data must represent a url to valid XML data.
If XML.BUFFER is used, then buffer must contain actual XML data.
In addition modifiers can be applied using the "+" operator.
XML.DEBUG can be added to display debug information during parsing.
If XML.ASYNC is added to XML.URL, then the connection is done asynchronously (the function returns immediately) and DOM availability can be later checked with the isNewData function.
In case of error, returns -1.
Example:


int open (String buffer,int mode, Function callback):
Same as open (String buffer, int mode), but will call a callback on completion.
This will force asynchronous mode (XML.ASYNC is implicitly added to mode).
The function will be called on completion of the parsing and must match the following signature:

Since 1.4.1

void close (int id):
Free all resources associated to a DOM. The value id cannot be used anymore after this call. You should use this function when you are done with the XML data.

bool isNewData (int id):
Returns true when new data just become available (i.e. when ASYNC is used). A second call to this function return false if no new data has arrived.

bool find (int id, String path):
Try to follow the path and keep a marker to the resulting element. return true on success and false when the path cannot be matched.
Path must follow a specific syntax that is supposed to be closed to XPATH:

/ represents a branch in the DOM
foo represents a node (tag)
[n] represents the nth occurrence (the first one is at 1)
* represents any node
:root represents the root
:parentrepresents the parent node of the current node
:next represents the next sibling of the current node

if path starts with "/" the path is absolute, otherwise the path starts from the previous find result. Once a node as been marked, its value, attributes or children can me retrieved using dedicated functions
Example:

String getValue (int id):
Returns the tag name or the CDATA of the current node (marked by a previous find command).

int getNbAttributes (int id):
Returns the number of attributes of the current node.

String getAttrName (int id, int n):
Returns the name of the nth attribute if the current node is a tag (and thus not a CDATA), with at least n attributes.
Returns an empty string otherwise.

String getAttrValue (int id, int n):
Returns the value of the nth attribute if the current node is a tag (and thus not a CDATA), with at least n attributes.
Returns an empty string otherwise.

String getAttrValue (int id, String name):
Returns the value of the first attribute that has the specified name, if the current node is a tag (and thus not a CDATA).
Returns an empty string otherwise.

int getNbChildren (int id):
Returns the number of children of the current node if it is a tag (and thus not a CDATA).
Returns 0 otherwise.

bool isTextChild (int id, int index):
Returns true if the current node has a child of type text (CDATA) at the specified index (starting at 1).

String getChildValue (int id, int index):
Returns the name of the tag or the CDATA of the child of current node at specified index (starting at 1).
Returns an empty string otherwise.

bool isSelfClosing (int id):
Returns true if the current node is self closing (i.e. <selfclosingtag />).

bool isSelfClosingChild (int id, int index):
Returns true if the current node has a self closing child at the specified index (starting at 1).

bool setValue (int id, String value):
Update with value, the content of a CDATA node (or the name of a tag node).
Returns true on success.
Since 1.4.1 (but not available to widgets)

bool setChildValue (int id, int index, String value):
Update with value, the content of a CDATA child (or the name of a tag child) at specified index (starting at 1).
Returns true on success.
Since 1.4.1 (but not available to widgets)

bool setAttributeValue (int id, String name, String value):
Update the value of the first attribute that has the specified name (or add as new attribute if not found), if the current node is a tag (and thus not a CDATA).
Returns true on success.
Since 1.4.1 (but not available to widgets)

String serialize (int id):
Returns the XML serialization of the current node and its children (when marked by a previous XML.find() call), or the entire DOM tree.
Returns an empty string on failure.
Since 1.4.1 (but not available to widgets)

bool save (int id, String record):
Returns true if the current node and its children (when marked by a previous XML.find() call) or the entire DOM tree could be serialized and the saved in RMS in the give record entry. Previous entry is overwritten.
Returns false on failure.
Since 1.4.1 (but not available to widgets)


BUFFER:
Constant for using direct xml data in open method

URL:
Constant for using url in open method

BML_ENCODING:
Constant to tell the parser that the XML is encoded using BML (Binary XML).

DEBUG:
Constant to display debug information during parsing

ASYNC:
Constant for opening the connection in asynchronous mode

HTML:
Constant to make XML parser less strict and improve support for some HTML tags (not closing tags such as <img>, <hr> and <br>).


Example that prints a DOM and some values using direct access


Example that shows the XML.open() using a callback


Example that shows DOM editing