The XmlNode class allows XML data to be loaded and saved, as well as accessed as a tree data structure of XmlNodes.
// a simple parsing example
const xml = `
<?xml version='1.0'>
<sample>
<test1 prop1='value1'>Contents</test1>
<test2></test2>
<test3/>
</sample>
`;
const node = new XmlNode();
node.parse(xml);
alert(`Root tag name is: ${node.getNodeName()}`);
for (const child of node.getChildNodes()) {
alert(`child has a tag name of: ${child.getNodeName()}`);
if (child.hasAttributes()) {
const attrs = child.getAttributes();
for (const name in attrs) {
alert(`child with node name ${child.getNodeName()} ` +
`has an attribute ${name} with a value of ${attrs[name]}`);
}
}
} Adds an attribute to the current XmlNode, with the specified name and value. If the attribute name and value are not-empty and they are properly set, the function returns true. However, if the name or value are empty, or the name or value are not properly set, the function returns false.
A reference to the appended node
Appends an XmlNode to the current XmlNode, using either the specified XmlNode, or creating a new one with the specified name node_name and node_content. If no parameters are specified, an empty child is created and appended. If node_name is specified but node_value isn't specified, a child having node_name is created and appended, but without any content.
A reference to the cloned node
Clones this XmlNode and returns a reference to the new node. If deep is set to true, this node's children will also be cloned; otherwise, only the name, value and properties of this node will be cloned. By deep isn't specified, then by default the children will be cloned.
Returns an object with the names and values of the node's attributes
Returns an object with named elements corresponding to the attribute names of the node. Each of these elements, in turn, contain the string value associated with each attribute
The XmlNode child corresponding to the input index or name, or null if no corresponding child exists.
Gets an XmlNode child of the current XmlNode from a specified index or name. The function returns the given XmlNode corresponding to the index or the name. However, the function returns null if the index is not in the valid range of indexes for the child XmlNodes, or the name is not a valid name of a child.
The number of XmlNode children for the current XmlNode.
Returns the number of immediate XmlNode children for the current XmlNode.
An array of XmlNode objects representing all node children
Calling getChildNodes() returns an array of XmlNode objects which represent all children of the node.
Returns the name for the current XmlNode.
Returns the value of the current XmlNode.
Returns the value of the current XmlNode.
True if the node has any attributes, false otherwise
True if the node has any child nodes, false otherwise
Returns true if the current XmlNode is empty, and false otherwise.
Indicates whether the current XmlNode is empty or not. The function returns true if the current XmlNode is empty, and false otherwise.
Returns true if the input file is successfully loaded, and false otherwise.
Loads an XML file, specfied by input, into an XML data structure. If the input file is successfully loaded, the function returns true. If the input file cannot be loaded, the function returns false.
Returns true if the text is successfully parsed into an XML data structure, and false otherwise.
Parses the input text to create an XML data structure. If text is parsed successfully, the function returns true. If text cannot be parsed, the function returns false.
Returns true if the XmlNode child is deleted, and false otherwise.
Deletes an XmlNode child of the current XmlNode at the specified index. If the child node is successfully deleted, the function returns true. If the child not is not deleted, the function returns false.
Returns true if the XML data is successfully saved to the output file, and false otherwise.
Saves an XML data structure to a file, specified by output. If the XML data is successfully saved to the output, file, the function returns true. If the XML data is not successfully saved to the output file, the function returns false.
Returns a string representation of an XML data structure.
Saves an XML data structure to a string.
Sets the name for the current XmlNode from the specified name.
Sets the value of the current XmlNode.