Summary:
See also: Classes and Objects, XML Utilities
The XmlReader class provides methods to read and process a file written in XML format, following the SAX standards.
om.XmlReader
Class Methods | |
Name | Description |
createFileReader( file STRING ) |
Creates an XmlReader reading from a file. |
Object Methods | |
Name | Description |
getAttributes() |
Returns the attribute list of the current element. |
getCharacters() |
Returns the string value of the current text element. |
skippedEntity() |
Returns the name of the entity. |
getTagName() |
Returns the tag name of the current element. |
read() |
Reads the next XML fragment and returns the name of the SAX event that
occurs. This can be one of StartDocument , StartElement ,
Characters , EndElement , EndDocument . |
The processing of the XML file is streamed-data based; the file is loaded and processed sequentially with events. To process element attributes, an XmlReader object must cooperate with a SaxAttributes object. The XmlReader class can only read from a file. To write to a file, you must use the XmlWriter class.
First, you must declare a variable of type om.XmlReader, and use the om.XmlReader.createFileReader(filename)
method to create the object; where filename is a
string expression defining the
name of the file to be read.
As with the standard SAX API,
the XML file is parsed on the basis of
events. Once the XmlReader object is created with the om.XmlReader.createFileReader()
method, you can successively call the
read()
method to get named events indicating how to parse
the XML file.
The following events can be returned by the read()
method:
Event name | Description | Action |
StartDocument |
Beginning of the document | Prepare processing (allocate resources) |
StartElement |
Beginning of a node | Get current element's tagname or attributes XmlReader.getTagName() XmlReader.getAttributes() |
Characters |
Value of the current element | Get current element's value XmlReader.getCharacters() |
SkippedEntity |
Name of the entity | Get current element's value XmlReader.skippedEntity() |
EndElement |
Ending of a node | Get current element's tagname XmlReader.getTagName() |
EndDocument |
Ending of the document | Finish processing (release resources) |
To process element attributes, you must declare a variable
of type SaxAttributes. This object represents a set of attributes of an element. You get an object
of this class with the getAttributes()
method.
Once created from the XmlReader, the SaxAttributes
object is automatically updated based on the element currently processed by
the XmlReader.
01
MAIN02
DEFINE i, l INTEGER03
DEFINE r om.XmlReader04
DEFINE e String05
DEFINE a om.SaxAttributes06
LET r = om.XmlReader.createFileReader("myfile.xml")07
LET a = r.getAttributes()08
LET l = 009
LET e = r.read()10
WHILE e IS NOT NULL11
CASE e12
WHEN "StartDocument"13
DISPLAY "StartDocument:"14
WHEN "StartElement"15
LET l=l+116
DISPLAY l SPACES, "StartElement:", r.getTagName()17
FOR i=1 to a.getLength()18
DISPLAY l SPACES," ",19
a.getName(i)," = ",20
a.getValueByIndex(i)21
END FOR22
WHEN "Characters"23
DISPLAY l SPACES, " Characters:'",r.getCharacters(),"'"24
WHEN "EndElement"25
DISPLAY l SPACES, "EndElement:", r.getTagName()26
LET l=l-127
WHEN "EndDocument"28
DISPLAY "EndDocument:"29
OTHERWISE30
DISPLAY "Invalid event: ",e31
END CASE32
LET e=r.read()33
END WHILE34
END MAIN