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

请帮助我们完善必备文件

我们非常欢迎您发送宝贵的反馈意见

请告诉我们什么主题需要更好的解释或如何才能更好的对其诠释。

分享这篇文章: Twitter | Facebook | LinkedIn | Email