xmlparse
xmlparse |
Purpose
Parse an XML document.
Synopsis
function xmlparse(afct:array(range) of string,mode:integer,ctx:ctxtype): integer
Arguments
afct
|
Event function table. Each entry of this array is the name of the function to call when the corresponding event occurs. The expected events are (all of these entries are optional):
|
||||||||||||||||
mode
|
If
0, spaces are preserved and returned as text elements. Otherwise all text elements are trimmed
|
||||||||||||||||
ctx
|
Value passed as first argument of all event functions
|
Return value
0 if successful,
1 in case of parsing error or a non-zero value returned by an event function
Example
This example displays the structure of an XML document without loading it into memory.
! Display element name and update indentation public function start_elt(spce:text,name:text,nba:integer):integer writeln(spce,name) spce+=" " end-function ! Update indentation when element closes public function end_elt(spce:text):integer spce-=" " end-function declarations afct:array(range) of string end-declarations afct(XML_FCT_OPEN_ELT):="start_elt" ! define open element afct(XML_FCT_CLOSE_ELT):="end_elt" ! define close element fopen("mydocument.xml",F_INPUT) rts:=xmlparse(afct,1,text("")) fclose(F_INPUT)
Further information
1. This function is an alternative approach to
load for processing XML documents: instead of loading into memory the entire document this function calls a dedicated routine whenever it identifies an XML entity. For instance a specific function is called when an element is open and another one when it is closed. It is up to the Mosel program to decide how to handle the document via these
event handling functions.
2. To each event type corresponds a specific function signature. These functions return an integer that decides whether parsing should continue: a non-zero value will cause the parsing to cancel (this value is used as the return value of
xmlparse). The expected function signatures are:
The type passed to the text node functions is the XML type corresponding to the function (namely XML_TXT, XML_CDATA, XML_COM, XML_DATA).
The open_element function receives the name of the element as well as the number of defined attributes. To retrieve these attributes xmlattr can be used.
- XML_FCT_DECL
- function xmldecl(ctx:ctxtype, vers:text, enc:text, std:integer):integer
- XML_FCT_TXT
- function text_node(ctx:ctxtype, type:integer, data:text):integer
- XML_FCT_CDATA
- function cdata_node(ctx:ctxtype, type:integer, data:text):integer
- XML_FCT_COM
- function comment_node(ctx:ctxtype, type:integer, data:text):integer
- XML_FCT_DATA
- function data_node(ctx:ctxtype, type:integer, data:text):integer
- XML_FCT_PINST
- function processing_instr(ctx:ctxtype, target:text, data:text):integer
- XML_FCT_OPEN_ELT
- function open_element(ctx:ctxtype, name:text, nba:integer):integer
- XML_FCT_CLOSE_ELT
- function close_element(ctx:ctxtype):integer
The type passed to the text node functions is the XML type corresponding to the function (namely XML_TXT, XML_CDATA, XML_COM, XML_DATA).
The open_element function receives the name of the element as well as the number of defined attributes. To retrieve these attributes xmlattr can be used.
3. An error message indicating the location of the error is displayed when the parsing fails or if an event function returns a negative value (a positive value also interrupts parsing but no message is displayed).
Related topics
Module