Sax v1.0.0 documentation

Documentation generated by XL CLAIRE v3.3.37 at Fri, 17 Nov 2006

Category index

  1. SAX parsing

Method index


Sax categories


categories
SAX parsing

SAX parsing

Sax is a simple efficient SAX parser (Simple API for XML). It is used to parse simple XML stream in an event oriented way. It is fully written in CLAIRE (it has no dependency on a third party library).

Here is a sample that shows how to use the parser. Let us consider the following XML that is inserted in a blob :

 b :: blob!("
     <company name='XL'>
         <employee name='Sylvain'>
             <email>s.benilan@claire-language.com</email>
         </employee>
     </company>
     "
)
 
We associate to this simple XML a simple CLAIRE class model :

 employee <: object  // predef

 company <: object(
             name:string,
             employees:list[employee])

 employee <: object(
             name:string,
             email:string)
 
We now have to define an event responder for the company element. Notice that, in CLAIRE, we may define a really precise domain for our event responder (the dynamic dispatch engine of CLAIRE is smart enought to find the appropriate restriction). So we can define a restriction of xml_enter that takes precisely an element with name "company" :

 xml_enter(data:list[company], elem:{"company"}, attrs:table: company ->
     let c := company(name = attrs["name"])
     in (data add c,
      c)
 xml_leave(data:list[company], elem:{"company"}, cdata:string: employee ->
     none
 
Note that we return a new instance of company, this instance will be used as the data transmited to company's sub elements (i.e. emloyees). Then we need a responder for the employee element :

 xml_enter(data:companyelem:{"employee"}, attrs:table: employee ->
     let e := employee(name = attrs["name"])
     in (data.employee add e,
         e)
 xml_leave(data:companyelem:{"employee"}, cdata:string: employee ->
     none
 
Here, the domain of our responder is more precise again since we only want a data that is of range company. Then we need a responder for the email property of an employee, we'll define here a responder for an element 'leave' event :

 xml_enter(data:employeeelem:{"email"}, attrs:table: void ->
     none
 xml_leave(data:employeeelem:{"email"}, cdata:string: void ->
     (data.email := cdata)
 
Now that our model is defined, we may invoke the SAX parser which is achieved with :

 let companies := list[company]
 in (Sax/sax(bxml_enterxml_leavecompanies),
         ... // something that uses companies
         )
 


Sax methods


categories SAX parsing inline Sax method

Sax/sax(self:port, xml_begin_element:property, xml_end_element:property) => any

sax(p,xml_begin_element, xml_end_element) is equivalent to sax(p,xml_begin_element, xml_end_element, unknown).


categories SAX parsing normal dispatch Sax method

Sax/sax(p:port, xml_begin_element:property, xml_end_element:property, data:any) -> any

sax(p, xml_begin_element, xml_end_element, data) read on the port p an XML stream in an event oriented way. Each time an element is entered (resp. leaved) the property xml_begin_element (resp. xml_end_element) is applied. The user code should define the appropriate restrictions of given properties as a receiver of generated events. The given data will be sent to event receivers and the returned value of an enter receiver will be used as the data sent to receivers of child elements. The event receiver should be defined with the following prototype :

 xml_begin_element(data:anyelement_name:stringattributes:table-> any
 xml_end_element(data:anyelement_name:stringcdata:string-> void
 
Where attributes is a table containing the attributes of an XML element which associates an attribute name to its value, both of range string. cdata is a string that contain the string data of an XML element.

If data is unknown then the prototype of receivers is simplified in :

 xml_begin_element(element_name:stringattributes:table-> void
 xml_end_element(element_name:stringcdata:string-> void
 


categories SAX parsing normal dispatch Sax method

Sax/set_handler(xml_begin_element:property, xml_end_element:property) -> void

set_handlers(xml_begin_element, xml_end_element) allows to change the selector of the event receivers from within an event receiver.