pyxser — Python XML Serialization

Overview

pyxser stands for python xml serialization and is a python object to XML serializer that validates every XML deserialization against the pyxser 1.0 DTD. It uses an O(n) algorithm for serializing objects to XML and an O(n) algorithm to deserialize XML and convert back to python objects. pyxser is written entirely in C as a python extension. The current stable release is pyxser-0.2r and supported Python version comes from 2.5 to 2.6.

Documentation


getdtd(...)
 This function returns the pyxser 1.0 DTD location in your system as
 string.

getdtd_c14n(...)
 This function returns the pyxser 1.0 DTD for C14N location in your
 system as string.

serialize(...)
 Uses the next keyword argumens:
    obj     ->      python object to serialize
    enc     ->      xml serialization encoding
    depth   ->      node navigation depth
  
 Gets any object defined in a Python module as class as argument
 and serializes it as XML. The serialization model is based on the
 pyxser DTD 1.0. Objects are serialized as pyxs:obj element, collections
 are serialized as pyxs:col element (tuples, lists and dictionaries)
 and properties are serialized as pyxs:prop elements.
  
 The serialization algorithm is a O(n) one, this means which the
 serializer runs over the object tree just one time and cross
 refernces are serialized as XML references through the IDREF
 attribute pyxs:ref.
  
 The serialization model resides in the pyxser public identifier DTD:
     <!DOCTYPE pyxs:obj
               PUBLIC '-//coder.cl//DTD pyxser 1.0//EN'
               'http://projects.coder.cl/pyxser/dtd/pyxser-1.0.dtd'>
 Returns an Unicode Object

serialize_c14n(...)
 Uses the next keyword argumens:
    obj     ->      python object to serialize
    depth   ->      node navigation depth
    exc     ->      exclusive canonization
    com     ->      with comments
  
 Same as serialize(), but uses C14N canonization, to use exclusive
 canonization the 'exc' argument must differ from zero and to use
 comments 'com' must differ from zero. The encoding must be UTF-8
 for canonization.
  
     <!DOCTYPE pyxs:obj
               PUBLIC '-//coder.cl//DTD pyxser C14N 1.0//EN'
               'http://projects.coder.cl/pyxser/dtd/pyxser-1.0-c14n.dtd'>
  
 * Information about Canonical XML at:
         http://www.w3.org/TR/xml-c14n
 * Information about Exclusive Canonical XML at
         http://www.w3.org/TR/xml-exc-c14n
 NOTE: The canonical DTD converts all ID, IDREF and NMTOKEN
       attributes to CDATA attributes
 Returns an Unicode Object

serialize_c14n_strict(...)
 Uses the next keyword argumens:
    obj     ->      python object to serialize
    depth   ->      node navigation depth
    exc     ->      exclusive canonization
    com     ->      with comments
  
 Same as serialize_c14n(), but uses C14N canonization in a strict
 mode and rather than serializing NMTOKEN, ID, and IDREF attributes,
 uses the C14N canon to execute the XML tree rendering. The encoding
 must be UTF-8 for canonization.
  
     <!DOCTYPE pyxs:obj
               PUBLIC '-//coder.cl//DTD pyxser C14N 1.0//EN'
               'http://projects.coder.cl/pyxser/dtd/pyxser-1.0.dtd'>
  
 * Information about Canonical XML at:
         http://www.w3.org/TR/xml-c14n
 * Information about Exclusive Canonical XML at
         http://www.w3.org/TR/xml-exc-c14n
 NOTE: C14N serialized objects can not be deserialized because we
       need the ID and IDREF attributes to suppor cross referenced
       objects.
 Returns an Unicode Object

unserialize(...)
 Uses the next keyword argumens:
    obj     ->      the serialized python object
    enc     ->      xml serialization encoding
 Takes an XML string as arguments to deserialize it and be converted
 back to it's original Python object. The deserialization algorithm
 supports automatic module loading, but searches for them in the module
 dictionary first to reach the original object type. It needs that the
 implied modules can be recheable by Python to get back the objects in
 it's original form.
  
 The deserialization algorithm is a O(n) one, this means that forward
 references are not supported because the first ocurrence of any object
 it's serialized once and then referenced, but not referenced and then
 serilized.
  
 Every serilized object is validated against the pyxser DTD 1.0

validate(...)
 Uses the next keyword argumens:
    obj     ->      the serialized python object
    enc     ->      xml serialization encoding
  
 Validates the XML input string against the pyxser DTD 1.0 in your
 local filesystem.

validate_c14n(...)
 Uses the next keyword argumens:
    obj     ->      the serialized python object
    enc     ->      xml serialization encoding
  
 Validates the XML input string against the pyxser DTD 1.0 C14N in
 your local filesystem.

xmlcleanup(...)
 Calls the cleanup function for the libxml2 parser, be carefull
 using this function, and try to use it when the parser really
 isn't in use.

Example output



<?xml version="1.0" encoding="UTF-8"?>
<pyxs:obj xmlns:pyxs="http://projects.coder.cl/pyxser/model/" version="1.0" type="ParentObject" module="testpkg.sample" objid="id-136863532">
  <pyxs:prop type="unicode" name="dyn_prop4" size="28">sm txt  Unicod</pyxs:prop>
  <pyxs:prop type="unicode" name="dyn_prop5" size="31">Aother Tex I ncDc</pyxs:prop>
  <pyxs:prop type="float" name="dyn_prop6">1.5</pyxs:prop>
  <pyxs:prop type="int" name="dyn_prop7">1000</pyxs:prop>
  <pyxs:col type="list" name="dyn_prop1">
    <pyxs:prop type="unicode" size="5">hol</pyxs:prop>
    <pyxs:prop type="unicode" size="5">cha</pyxs:prop>
    <pyxs:obj type="TestAnotherObject" module="testpkg.sample" objid="id-136773868">
      <pyxs:prop type="int" name="first_element">123</pyxs:prop>
      <pyxs:obj module="testpkg.sample" type="TestAnotherObject" name="dyn_prop1" objid="id-136863372">
        <pyxs:prop type="str" name="first_element">xyz</pyxs:prop>
        <pyxs:prop type="complex" name="second_element">7.810000:2.010000</pyxs:prop>
      </pyxs:obj>
      <pyxs:prop type="int" name="second_element">456</pyxs:prop>
    </pyxs:obj>
  </pyxs:col>
  <pyxs:col type="tuple" name="dyn_prop2">
    <pyxs:prop type="unicode" size="4">hol`</pyxs:prop>
    <pyxs:prop type="unicode" size="4">sn</pyxs:prop>
    <pyxs:prop type="str">trip</pyxs:prop>
    <pyxs:obj type="TestAnotherObject" module="testpkg.sample" objid="id-136863404">
      <pyxs:prop type="complex" name="first_element">5.820000:3.990000</pyxs:prop>
      <pyxs:prop type="str" name="second_element">cdf</pyxs:prop>
    </pyxs:obj>
  </pyxs:col>
  <pyxs:col type="dict" name="dyn_prop3">
    <pyxs:prop type="unicode" name="saludo1" size="6">hl</pyxs:prop>
    <pyxs:prop type="unicode" name="saludo2" size="6">ch</pyxs:prop>
    <pyxs:obj type="TestAnotherObject" name="saludo5" module="testpkg.sample" objid="id-136863500">
      <pyxs:prop type="str" name="first_element">XXX</pyxs:prop>
      <pyxs:prop type="complex" name="second_element">32.100000:33.100000</pyxs:prop>
    </pyxs:obj>
    <pyxs:prop type="str" name="saludo4">goodbye</pyxs:prop>
  </pyxs:col>
  <pyxs:obj module="testpkg.sample" type="ChildObject" name="parent3" objid="id-136863660">
    <pyxs:prop type="str" name="child1">hi</pyxs:prop>
    <pyxs:prop type="float" name="child2">4.5</pyxs:prop>
    <pyxs:prop type="int" name="child3">2354</pyxs:prop>
    <pyxs:obj module="testpkg.sample" type="NestedChild" name="child4" objid="id-136863692">
      <pyxs:obj module="testpkg.sample" type="SubNestedChild" name="nested4" objid="id-136863724">
        <pyxs:obj module="testpkg.sample" type="TestAnotherObject" name="subnested1" objref="id-136863404"/>
      </pyxs:obj>
      <pyxs:prop type="str" name="nested1">holahola</pyxs:prop>
      <pyxs:prop type="int" name="nested2">345</pyxs:prop>
      <pyxs:prop type="str" name="nested3">hola</pyxs:prop>
    </pyxs:obj>
  </pyxs:obj>
  <pyxs:prop type="str" name="parent2">chao</pyxs:prop>
  <pyxs:prop type="str" name="parent1">hola</pyxs:prop>
</pyxs:obj>

Features

License

The pyxser licence is GNU Lesser General Public License version 3. You can use in your closed source application, but if you modify pyxser, you must publish the changes.