The XML datas should be in UTF-8 format if the encoding attribute is not present. Otherwise, define it into the XML file/datas or use _sax2EncAsk.
Note that the reader runs on a specific child thread.
To make a basic parser, you must :
See the example below.
The Scol type to use with this api. .........
Create an object from a file (within the active partition). The content is not loaded in the memory.
Its prototype :
Return : a new Scol object
Create an object from a string.
Its prototype :
Return : a new Scol object
This is called when the reader starts the parsing. It's useful to initialize any variable, structure or more to store any results.
Its prototype :
Return the same object
This is called when the reader ends the parsing.
Its prototype :
Return the same object
This is called when the reader starts a new element
Its prototype :
Return the same object
Example :
<id number="121" name="bob" mail="bob@test.com" />
will return :
_name_ : id
_list_ : number:121:name:bob:mail:bob@test.com:NIL
Thi is called when the reader ends an element
Its prototype :
Return the same object
Thiis called when the reader receiving some chars.
Its prototype :
Return the same object
This is called when the reader meets any comments.
Its prototype :
Return the same object
This is called when the reader meets an entity.
Its prototype :
Return the same object
Note : the content of the entity can be retrieved with _sax2CBcharacters. Ok, it's not perfect yet (ToDo)
This is called when an entity reference is detected.
Its prototype :
Return the same object
This is called when a pcdata block has been parsed.
Its prototype :
Return the same object
Called when the reading is finished AND the Scol object is destroyed. The child thread is destroying.
It is useful to continue a program part being sure that the xml threatment is ended.
Its prototype :
Return the same object
Reads / Parses an valid Scol object
Its prototype :
Return 0 if success
This function allows to define an encoding conversion. It must call before the reader. This function is experimental yet.
Its prototype :
Return the same Scol object.
This example parses the definition file of the "serial" plugIt (Openspace3d).
The result is displayed in the console and the log file (log mask : Foo or more).
This file can be found here (if Openspace3d is installed) : "tools\os3dplugins\input\serial\serial.xml"
startDocument, endDocumet, startElement and endElement callbacks are used.
First, the Scol source code :
typeof xml = ObjSax2;; // callback when a new element starts fun cbElementStart (obj, user_parameter, name, attrs)= _fooS up; _fooS strcat "element = " name; _fooSList attrs; 0;; // callback when an element ends fun cbElementEnd (obj, user_parameter, name)= _fooS up; _fooS strcat "element = " name; 0;; // callback to the beginning (the ending) of a xml document fun cbDocument (obj, user_parameter)= _fooS if user_parameter then "END DOCUMENT" else "BEGIN DOCUMENT"; 0;; // 1 : create a new ObjSax2 object, 2 : define the callbacks, 3 : launch the reader fun xmlParse ()= set xml = _sax2CreateFromFile _channel _checkpack "tools\os3dplugins\input\serial\serial.xml"; _sax2CBelementStart xml @cbElementStart "startElement"; _sax2CBelementEnd xml @cbElementEnd "endElement"; _sax2CBdocumentStart xml @cbDocument 0; _sax2CBdocumentEnd xml @cbDocument 1; _sax2Read xml; 0;;
Next, the log file should be ...:
BEGIN DOCUMENT startElement element = PLUGIN name:serial:version:1.0:type:input:NIL startElement element = DESCRIPTION NIL endElement element = DESCRIPTION startElement element = HELP NIL endElement element = HELP startElement element = RESOURCE NIL endElement element = RESOURCE startElement element = EDITOR NIL startElement element = SCRIPT path:./eserial.pkg:NIL endElement element = SCRIPT startElement element = PARAM name:port:type:value:NIL endElement element = PARAM startElement element = PARAM name:speed:type:value:NIL endElement element = PARAM startElement element = PARAM name:bytesize:type:value:NIL endElement element = PARAM startElement element = PARAM name:parity:type:value:NIL endElement element = PARAM startElement element = PARAM name:stopbits:type:value:NIL endElement element = PARAM startElement element = PARAM name:lastchar:type:value:NIL endElement element = PARAM endElement element = EDITOR startElement element = CLIENT minstance:true:NIL startElement element = SCRIPT path:./cserial.pkg:NIL endElement element = SCRIPT startElement element = ACTION name:Send:NIL endElement element = ACTION startElement element = EVENT name:Read:NIL endElement element = EVENT endElement element = CLIENT endElement element = PLUGIN END DOCUMENT
This example is similar than the previous. We get a content from internet, we define the callbacks and we launch the reader. The result is a log file / console.
The Scol source code :
typeof xml = ObjSax2;; typeof datasFromUrl = S;; fun cbElementStart (obj, user_parameter, name, attrs)= _fooS up; _fooS strcat "element = " name; _fooSList attrs; 0;; fun cbElementEnd (obj, user_parameter, name)= _fooS up; _fooS strcat "element = " name; 0;; fun xmlParse ()= set xml = _sax2CreateFromString _channel datasFromUrl; _sax2CBelementStart xml @cbElementStart "startElement"; _sax2CBelementEnd xml @cbElementEnd "endElement"; _sax2CBdocumentStart xml @cbDocument 0; _sax2CBdocumentEnd xml @cbDocument 1; _sax2Read xml; 0;; fun CBgetDatasFromUrl (obj, user_parameter, data, err)= if err == 0 then ( set datasFromUrl = strcat datasFromUrl data; 0 ) else if err == 1 then xmlParse else ( _fooS strcat "error CBgetDatasFromUrl : " itoa err; 0 );; fun getDatasFromUrl ()= INETGetURL _channel "http://www.google.com/ig/api?weather=marseille&hl=fr" 0 @CBgetDatasFromUrl 0; 0;; fun main ()= _showconsole; getDatasFromUrl; 0;;
author : iri, june 2011