Banana.Xml

This documentation is outdated

The most complete and up-to-date documentation is the one of Banana Accounting Plus: Try it now

In this article

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

Share this article: Twitter | Facebook | LinkedIn | Email