xml_doc = open('test.xml').read() objectList1 = d1common.types.objectlist_serialization.CreateFromDocument(xml_doc, 'text/xml') print objectList1.toxml() json, content_type = objectList1.serialize('application/json') objectList2 = d1common.types.objectlist_serialization.CreateFromDocument(json, 'application/json') print '\n' + objectList2.toxml() CreateFromDocument with 'text/xml' goes to the superclass and creates an object directly from there, and then casts it to the subclass. The CreateFromDocument call with 'application/json' create creates a subclass directly. The JSON object itself does not contain any information about the name of the root object. ------- hdl:10255/dryad.1228/mets.xmleml://ecoinformatics.org/eml-2.0.03f56de593b6ffc536253b799b429453e3673fc191970-01-19T04:53:32Z3666hdl:10255/dryad.1073/mets.xmleml://ecoinformatics.org/eml-2.0.0aac6cca196fb6330d1013a10cce6a4604ca180d31970-02-06T18:11:22Z3635AnserMatrix.htmeml://ecoinformatics.org/eml-2.0.00e25cf59d7bd4d57154cc83e0aa32b341970-05-27T06:12:49Z11048 hdl:10255/dryad.1228/mets.xmleml://ecoinformatics.org/eml-2.0.03f56de593b6ffc536253b799b429453e3673fc191970-01-19T04:53:32Z3666hdl:10255/dryad.1073/mets.xmleml://ecoinformatics.org/eml-2.0.0aac6cca196fb6330d1013a10cce6a4604ca180d31970-02-06T18:11:22Z3635AnserMatrix.htmeml://ecoinformatics.org/eml-2.0.00e25cf59d7bd4d57154cc83e0aa32b341970-05-27T06:12:49Z11048 In [1]: import objectlist incorrect: In [2]: ol = objectlist.ObjectList() In [3]: ol.toxml Out[3]: > In [4]: ol.toxml() Out[4]: u'' In [5]: x = ol.toxml() In [6]: ol2 = objectlist.Cre objectlist.CreateFromDOM objectlist.CreateFromDocument In [6]: ol2 = objectlist.CreateFromDocument(x) ------------------------------------------------------------ Traceback (most recent call last): File "", line 1, in File "/Users/vieglais/Projects/D1_cicore/api-common-python/src/d1common/types/generated/objectlist.py", line 33, in CreateFromDocument instance = handler.rootObject() File "/Users/vieglais/lib/python/pyxb/binding/saxer.py", line 258, in rootObject raise pyxb.UnrecognizedElementError(dom_node=self.__rootObject) UnrecognizedElementError: UnrecognizedElementError: No element binding available for {http://dataone.org/service/types/ObjectList/0.1}ObjectList Correct: In [1]: import objectlist In [2]: ol = objectlist.objectList() In [3]: ol.toxml() Out[3]: u'' ================================================ ''' Extends the PyXB generated class to enable serialization to and from CSV and JSON. ''' import new import d1common import d1common.exceptions import d1common.types.generated.systemmetadata #=============================================================================== def toCSV(self, dialect='excel'): raise d1common.exceptions.NotImplemented('0.SystemMetaData.toCSV', 'Serializing SystemMetadata to CSV is not implemented.') def fromCSV(self, data, dialect='excel'): raise d1common.exceptions.NotImplemented('0.SystemMetaData.fromCSV', 'De-serializing SystemMetadata from CSV is not implemented.') def toJSON(self): raise d1common.exceptions.NotImplemented('0.SystemMetaData.toJSON', 'Serializing SystemMetadata to JSON is not implemented.') def fromJSON(self, data): raise d1common.exceptions.NotImplemented('0.SystemMetaData.fromJSON', 'De-serializing SystemMetadata from JSON is not implemented.') def systemMetadata(): sysm = d1common.types.generated.systemmetadata.systemMetadata() sysm.toCSV = new.instancemethod(toCSV, sysm, sysm.__class__) sysm.fromCSV = new.instancemethod(fromCSV, sysm, sysm.__class__) sysm.toJSON = new.instancemethod(toJSON, sysm, sysm.__class__) sysm.fromJSON = new.instancemethod(fromJSON, sysm, sysm.__class__) def CreateFromDocument(data, format='text/xml'): format = format.strip().lower() if format == d1common.MIMETYPES['xml']: sysm = d1common.types.generated.systemmetadata.CreateFromDocument( data ) sysm.toCSV = new.instancemethod(toCSV, sysm, sysm.__class__) sysm.fromCSV = new.instancemethod(fromCSV, sysm, sysm.__class__) sysm.toJSON = new.instancemethod(toJSON, sysm, sysm.__class__) sysm.fromJSON = new.instancemethod(fromJSON, sysm, sysm.__class__) return sysm if format == d1common.MIMETYPES['json']: res = systemMetadata() return res.fromJSON(data) if format == d1common.MIMETYPES['csv']: res = systemMetadata() return res.fromCSV(data) raise ValueError('Unknown ObjectList serialization format: %s' % format)