Banana.Xml

The Banana.Xml class is used to parse and access xml data.​

Since: Banana Accounting 9.0.5

Introduction

The API Banana.Xml and Banana.Xml.XmlElement implement a subset of the DOM Document Object Model interface.  The most used properties and methods are implemented.

For example the list of books in the following xml file:

<Library updated="2016-10-31">
   <Book>
      <Title>Paths of colours</Title>
      <Author>Rosa Indaco</Author>
   </Book>
   <Book>
      <Title>Accounting exercises</Title>
      <Author>Su Zhang</Author>
   </Book>
</Library>

Can be retrieved with the following code:

var xmlFile = Banana.Xml.parse(xml);
var xmlRoot = xmlFile.firstChildElement('Library');
var updateDate = xmlRoot.attribute('updated');
var bookNode = xmlRoot.firstChildElement('Book'); // First book
while (bookNode) {
   // For each book in the library
   var title = bookNode.firstChildElement('Title').text;
   var authorNode = bookNode.firstChildElement('Author');
   var author = authorNode ? authorNode.text : 'unknow';
   bookNode = bookNode.nextSiblingElement('Book'); // Next book
}

 

Properties

errorString

Read only. The string of the last occured error. If no error occured it is empty.

Since Banana 9.0.4

Methods

newDocument(name)

The method newDocument(name) creates a new Xml document and returns it as a Banana.Xml.XmlElment object.

Since Banana 9.0.4

parse(xml)

The method parse(xml) parses a xml data and returns an object of type Banana.Xml.XmlElment that represents the parsed xml. If the xml data is not valid, this method returns null, the occured error can be retrieved through the property errorString.

var xmlFile = Banana.Xml.parse(xml); 
var xmlRoot = xmlFile.firstChildElement('Bookshelf'); // The root element is named 'Bookshelf' in this example

save(xmlElement)

The method newDocument(name) returns a Banana.Xml.XmlElment as a string.

Since Banana 9.0.4

validate(xmlElement, schemaFilePath)

The method validate(xmlElement, schemaFilePath) validates a Banana.Xml.XmlElment against a shema. The schema is passed as a path relative to the script path. The method returns true if the validation passed, otherwise it returns false. The occured validation error can be retrieved though the property errorString.

// Create document
var xmlDocument = Banana.Xml.newDocument("eCH-0217:VATDeclaration");
var rootNode = xmlDocument.addElement("eCH-0217:VATDeclaration");
...
 
// Validate against schema (schema is passed as a file path relative to the script)
var schemaFileName = "eCH-0217-1-0.xsd";
if (Banana.Xml.validate(xmlDocument, schemaFileName)) {
   Banana.Ui.showInformation("Validation result", "Xml document is valid against " + schemaFileName);  
} else {
   Banana.Ui.showInformation("Validation result", "Xml document is not valid againts " + schemaFileName + ": " + Banana.Xml.errorString);
}

Since Banana 9.0.4

Banana.Xml.XmlElement

The XmlElement class represents an Xml element. See Banana.Xml for an example.

Since: Banana Accounting 9.0.3

Properties

nodeName

The read only property nodeName returns the node name of the xml element.

parent

The read only property parent returns the parent of this Xml element as a Banana.Xml.XmlElment object. If this is the root element, it returns null.

text

The read only property text returns the text of this Xml element and their childs.

value

This is a synomin of the property text.

Methods

addProcessingInstruction(target, data)

Adds a new processing instruction to the document.

xmlDoc.addProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"');

Since Banana 9.0.4

addElement(name)

Adds a new Banana.Xml.XmlElement with the specified name to the document and returns it.

Since Banana 9.0.4

addElementNs(ns, name)

Adds a new Banana.Xml.XmlElement with the specified name and namespace to the document and returns it.

Since Banana 9.0.4

addComment(text)

Adds a comment note to the document, and returns it as a Banana.Xml.XmlElement object.

Since Banana 9.0.4

addTextNode(text)

Adds a new Xml TextNode to the document and returns it as a Banana.Xml.XmlElement object.

Since Banana 9.0.4

attibute(name [, defaultValue])

Returns the value of the attribute with the specified name as a string. If no attibute with the specified name is found, the defaultValue or an empty string is returned.

attibuteNS(ns, name [, defaultValue])

Returns the value of the attribute with the specified name and namespace as a string. If no attibute with the specified name is found, the defaultValue or an empty string is returned.

elementsByTagName(name)

Returns an array containing all descendants of this element with the specified name.

firstChildElement([name])

Returns the first child element with the specified name if name is non-empty, otherwise it returns the first child element. Returns null if no such child exists.

var bookNode = xmlRoot.firstChildElement('Book');  // First book
while (bookNode) { 
   // For each book in the library
   var title = xmlFile.firstChildElement('Title').text();
   bookNode = bookNode.nextSiblingElement('Book');  // Next book
} 

hasChildElements([name])

Returns true if this element contains one or mode elemets with the specified name.

hasAttribute(name)

Returns true is the attribute with the specified name exists.

hasAttributeNS(ns, name)

Returns true if the attribute with the specified name and namespace exists.

lastChildElement([name])

Returns the last child element with the specified name if name is non-empty, otherwise it returns the last child element. Returns null if no such child exists.

namespaceURI()

Returns the namespace URI of this node or an empty string if the node has no namespace URI.

Since Banana 9.0.4

nextSiblingElement([name])

Returns the next sibling element with the specified name if name is non-empty, otherwise returns any next sibling element. Returns null if no such sibling exists.

prefix()

Returns the namespace prefix of the node or an empty string if the node has no namespace prefix.

Since Banana 9.0.4

previousSiblingElement([name])

Returns the previous sibling element with the specified name if name is non-empty, otherwise returns any previous sibling element. Returns null if no such sibling exists.

setAttribute(name, value)

Adds an attribute with the qualified name name with the value value.

Since Banana 9.0.4

setAttributeNs(ns, name, value)

Adds an attribute with the qualified name name and the namespace URI ns with the value value.

Since Banana 9.0.4

setPrefix(value)

If the node has a namespace prefix, this function changes the namespace prefix of the node to pre. Otherwise this function does nothing.

Since Banana 9.0.4