SCOL LibXml2 SAX2 api

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 :

  1. create of the Scol object;
  2. define of all needed callbacks (1 or more);
  3. launch the reader.

See the example below.

Scoltype

ObjSax2

The Scol type to use with this api. .........

Creation

_sax2CreateFromFile

Create an object from a file (within the active partition). The content is not loaded in the memory.

Its prototype :

fun [Chn P] ObjSax2

Return : a new Scol object

_sax2CreateFromString

Create an object from a string.

Its prototype :

fun [Chn S] ObjSax2

Return : a new Scol object

Callback

_sax2CBdocumentStart

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 :

fun [ObjSax2 fun [ObjSax2 u0] u1 u0] ObjSax2

Return the same object

_sax2CBdocumentEnd

This is called when the reader ends the parsing.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0] u1 u0] ObjSax2

Return the same object

_sax2CBelementStart

This is called when the reader starts a new element

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S [S r1]] u1 u0] ObjSax2

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

_sax2CBelementEnd

Thi is called when the reader ends an element

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S] u1 u0] ObjSax2

Return the same object

_sax2CBcharacters

Thiis called when the reader receiving some chars.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S] u1 u0] ObjSax2

Return the same object

_sax2CBcomment

This is called when the reader meets any comments.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S] u1 u0] ObjSax2

Return the same object

_sax2CBentity

This is called when the reader meets an entity.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S] u1 u0] ObjSax2

Return the same object

Note : the content of the entity can be retrieved with _sax2CBcharacters. Ok, it's not perfect yet (ToDo)

_sax2CBreference

This is called when an entity reference is detected.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S] u1 u0] ObjSax2

Return the same object

_sax2CBcdata

This is called when a pcdata block has been parsed.

Its prototype :

fun [ObjSax2 fun [ObjSax2 u0 S I] u1 u0] ObjSax2

Return the same object

_sax2CBdestroyed

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 :

fun [ObjSax2 fun [ObjSax2 u0] u1 u0] ObjSax2

Return the same object

Reader

_sax2Read

Reads / Parses an valid Scol object

Its prototype :

fun [ObjSax2] I

Return 0 if success

Others

_sax2EncAsk

This function allows to define an encoding conversion. It must call before the reader. This function is experimental yet.

Its prototype :

fun [ObjSax2 S S] ObjSax2

Return the same Scol object.

Examples :

1 - From a file

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

2 - From the internet

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