xml Module
Overview
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')
xml.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.
Property
Property | Type | R/W | Explanation |
---|---|---|---|
name |
string |
R | |
value |
string |
R |
xml.document Class
Constructor
xml.document(stream?:stream:r) {block?}
Property
Property | Type | R/W | Explanation |
---|---|---|---|
version |
string |
R | |
encoding |
string |
R | |
root |
xml.element |
R |
Method
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
xml.element Class
Constructor
xml.element(_tagname_:string, attrs%):map {block?}
xml.comment(comment:string)
Property
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. |
Method
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
xml.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()
Constructor
xml.parser() {block?}
Method
xml.parser#parse(stream:stream:r):void
Thanks
This module uses expat library which is distributed in the following site: