Banana.Xml
The Banana.Xml class is used to parse and access xml data.
Since: Banana Accounting 8.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 methos 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('Bookshelf'); var updateDate = xmlRoot.attribute('updated'); var bookNode = xmlRoot.firstChildElement('Book'); // First book while (bookNode) { // For each book in the library var title = xmlFile.firstChildElement('Title').text(); var authorNode = xmlFile.firstChildElement('Author'); var author = authorNode ? authorNode.text() : 'unknow'; bookNode = bookNode.nextSiblingElement('Book'); // Next book }
Methods
parse(xml)
The method parse(xml) parses 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.
var xmlFile = Banana.Xml.parse(xml); var xmlRoot = xmlFile.firstChildElement('Bookshelf'); // The root element is named 'Bookshelf' in this example
Banana.Xml.XmlElement
The XmlElement class represent an Xml element. See Banana.Xml for an example.
Since: Banana Accounting 8.0.5
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 return 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
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.
hasAttribute(name)
Returns true is the attribute with the specified name exists.
hasAttributeNS(ns, name)
Returns true is the attribute with the specified name and namespace exists.
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.
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.
elementsByTagName(name)
Returns an array containing all descendants of this element with the specified name.
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.
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.