59xml Module

59.1Overview

The xml module provides measures to parse or compose XML documents.

There are two ways to parse an XML document as follows.

One is to create an xml.document instance from a stream that contains all the XML elements with a tree structure. This is an easy way to parse an XML document but consumes much memory. Below is an example to read an XML file test.xml:

doc = xml.document('test.xml')
// doc contains all the information of XML document

Another one is to create a class inherited xml.parser and implements event handlers that respond to tags, comments and texts, and then executes xml.parser#parse() method with it. Below is an example to create a class that implements a handler for StartElement event:

Parser = class(xml.parser) {
    StartElement(elem) = {
        printf('<%s>\n', elem.tagname)
    }
}
Parser().parse('test.xml')

59.2xml.attribute Class

The xml.attribute instance represents a name-value pair of XML's attribute that can be retrieved from attrs property in the xml.element instance.

59.2.1Property

Property Type R/W Explanation
name string R
value string R

59.3xml.document Class

59.3.1Constructor

xml.document(stream?:stream:r) {block?}

59.3.2Property

Property Type R/W Explanation
version string R
encoding string R
root xml.element R

59.3.3Method

xml.document#parse(str:string):void
xml.document#read(stream:stream:r):void
xml.document#textize(fancy?:boolean, tabs?:number)
xml.document#write(stream:stream:w, fancy?:boolean, tabs?:number):void

59.4xml.element Class

59.4.1Constructor

xml.element(_tagname_:string, attrs%):map {block?}
xml.comment(comment:string)

59.4.2Property

Property Type R/W Explanation
tagname string R A tag name of this element.
text string R The text string if the element is TEXT. Otherwise, this value would be nil.
comment string R The comment string if the element is COMMENT. Otherwise, this value would be nil.
children iterator R An iterator to return xml.element instances that represent children contained in this element. This value would be nil if the element has no children.
attrs iterator R An iterator to return xml.attribute instances that represent attributes contained in this element. This value would be nil if the element has no attributes.

59.4.3Method

xml.element#addchild(value):map:void
xml.element#gettext()
xml.element#textize(fancy?:boolean, indentLevel?:number, tabs?:number)
xml.element#write(stream:stream:w, fancy?:boolean, indentLevel?:number, tabs?:number):void

59.5xml.parser Class

The xml.parser class is a base class from which you can implement a inheritance class that has methods corresponding to events associated with XML elements. Below are methods that you can implement in the class for event handling:

  • StartElement(elem:xml.element)
  • EndElement(name:string)
  • CharacterData(text:string)
  • ProcessingInstruction(target:string, data:string)
  • Comment(data:string)
  • StartCdataSection()
  • EndCdataSection()
  • Default(text:string)
  • DefaultExpand(text:string)
  • ExternalEntityRef()
  • SkippedEntity(entityName:string, isParameterEntity:boolean)
  • StartNamespaceDecl(prefix:string, uri:string)
  • EndNamespaceDecl(prefix:string)
  • XmlDecl(version:string, encoding:string, standalone:boolean)
  • StartDoctypeDecl(doctypeName:strng, systemId:string, publicId:string, hasInternalSubset:boolean)
  • EndDoctypeDecl()
  • ElementDecl()
  • AttlistDecl(elemName:string, attName:string, attType:string, defaultValue:string, isRequired:boolean)
  • EntityDecl(entityName:string, isParameterEntity:boolean, value:string, base:string, systemId:string, publicId:string, notationName:string)
  • NotationDecl(notationName:string, base:string, systemId:string, publicId:string)
  • NotStandalone()

59.5.1Constructor

xml.parser() {block?}

59.5.2Method

xml.parser#parse(stream:stream:r):void

59.6Thanks

This module uses expat library which is distributed in the following site:

http://expat.sourceforge.net/