def CreateFromDocument(xml_text, default_namespace=None, location_base=None): """Parse the given XML and use the document element to create a Python instance. @param xml_text An XML document. This should be data (Python 2 str or Python 3 bytes), or a text (Python 2 unicode or Python 3 str) in the L{pyxb._InputEncoding} encoding. @keyword default_namespace The L{pyxb.Namespace} instance to use as the default namespace where there is no default namespace in scope. If unspecified or C{None}, the namespace of the module containing this function will be used. @keyword location_base: An object to be recorded as the base of all L{pyxb.utils.utility.Location} instances associated with events and objects handled by the parser. You might pass the URI from which the document was obtained. """ if pyxb.XMLStyle_saxer != pyxb._XMLStyle: dom = pyxb.utils.domutils.StringToDOM(xml_text) return CreateFromDOM(dom.documentElement, default_namespace=default_namespace) if default_namespace is None: default_namespace = Namespace.fallbackNamespace() saxer = pyxb.binding.saxer.make_parser( fallback_namespace=default_namespace, location_base=location_base) handler = saxer.getContentHandler() xmld = xml_text if isinstance(xmld, _six.text_type): xmld = xmld.encode(pyxb._InputEncoding) saxer.parse(io.BytesIO(xmld)) instance = handler.rootObject() return instance
def CreateFromDocument (xml_text, default_namespace=None, location_base=None): """Parse the given XML and use the document element to create a Python instance. @param xml_text An XML document. This should be data (Python 2 str or Python 3 bytes), or a text (Python 2 unicode or Python 3 str) in the L{pyxb._InputEncoding} encoding. @keyword default_namespace The L{pyxb.Namespace} instance to use as the default namespace where there is no default namespace in scope. If unspecified or C{None}, the namespace of the module containing this function will be used. @keyword location_base: An object to be recorded as the base of all L{pyxb.utils.utility.Location} instances associated with events and objects handled by the parser. You might pass the URI from which the document was obtained. """ if pyxb.XMLStyle_saxer != pyxb._XMLStyle: dom = pyxb.utils.domutils.StringToDOM(xml_text) return CreateFromDOM(dom.documentElement) if default_namespace is None: default_namespace = Namespace.fallbackNamespace() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=default_namespace, location_base=location_base) handler = saxer.getContentHandler() xmld = xml_text if isinstance(xmld, unicode): xmld = xmld.encode(pyxb._InputEncoding) saxer.parse(io.BytesIO(xmld)) instance = handler.rootObject() return instance
def CreateFromDocument (xml_text, default_namespace=None, location_base=None): """Parse the given XML and use the document element to create a Python instance. @kw default_namespace The L{pyxb.Namespace} instance to use as the default namespace where there is no default namespace in scope. If unspecified or C{None}, the namespace of the module containing this function will be used. @keyword location_base: An object to be recorded as the base of all L{pyxb.utils.utility.Location} instances associated with events and objects handled by the parser. You might pass the URI from which the document was obtained. """ if pyxb.XMLStyle_saxer != pyxb._XMLStyle: dom = pyxb.utils.domutils.StringToDOM(xml_text) return CreateFromDOM(dom.documentElement) if default_namespace is None: default_namespace = Namespace.fallbackNamespace() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=default_namespace, location_base=location_base) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml_text)) instance = handler.rootObject() return instance
def CreateFromDocument (xml_text, default_namespace=None, location_base=None): """Parse the given XML and use the document element to create a Python instance.""" if pyxb.XMLStyle_saxer != pyxb._XMLStyle: dom = pyxb.utils.domutils.StringToDOM(xml_text) return CreateFromDOM(dom.documentElement) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace.fallbackNamespace(), location_base=location_base) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml_text)) instance = handler.rootObject() return instance
def testISO8601 (self): xml = '<when><ISO8601>2009-06-15T17:50:00Z</ISO8601></when>' dom = pyxb.utils.domutils.StringToDOM(xml) instance = CreateFromDOM(dom.documentElement) self.assertEqual(instance.sgTime._element(), ISO8601) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xml) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml)) instance = handler.rootObject() self.assertEqual(instance.sgTime._element(), ISO8601) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xml)
def testOptionalNilSETag(self): xmlt = u'<optional xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></optional>' doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance, '') self.assertTrue(instance._isNil()) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance, '') self.assertTrue(instance._isNil())
def testOptionalNilSETag (self): xmlt = u'<optional xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></optional>' doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance, '') self.assertTrue(instance._isNil()) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance, '') self.assertTrue(instance._isNil())
def testISO8601(self): xml = '<when><ISO8601>2009-06-15T17:50:00Z</ISO8601></when>' dom = pyxb.utils.domutils.StringToDOM(xml) instance = CreateFromDOM(dom.documentElement) self.assertEqual(instance.sgTime._element(), ISO8601) self.assertEqual(instance.toDOM().documentElement.toxml(), xml) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml)) instance = handler.rootObject() self.assertEqual(instance.sgTime._element(), ISO8601) self.assertEqual(instance.toDOM().documentElement.toxml(), xml)
def testPairTime(self): xml = '<when><pairTime><seconds>34.0</seconds><fractionalSeconds>0.21</fractionalSeconds></pairTime></when>' dom = pyxb.utils.domutils.StringToDOM(xml) instance = CreateFromDOM(dom.documentElement) self.assertEqual(instance.sgTime._element(), pairTime) self.assertEqual(instance.sgTime.seconds, 34) self.assertEqual(instance.toDOM().documentElement.toxml(), xml) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml)) instance = handler.rootObject() self.assertEqual(instance.sgTime._element(), pairTime) self.assertEqual(instance.sgTime.seconds, 34) self.assertEqual(instance.toDOM().documentElement.toxml(), xml)
def CreateFromDocument (xml_text, default_namespace=None, location_base=None): if pyxb.XMLStyle_saxer != pyxb._XMLStyle: dom = pyxb.utils.domutils.StringToDOM(xml_text) return CreateFromDOM(dom.documentElement, default_namespace=default_namespace) if default_namespace is None: default_namespace = Namespace.fallbackNamespace() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=default_namespace, location_base=location_base) handler = saxer.getContentHandler() xmld = xml_text if isinstance(xmld, _six.text_type): xmld = xmld.encode(pyxb._InputEncoding) saxer.parse(io.BytesIO(xmld)) instance = handler.rootObject() return instance
def testPairTime (self): xml = '<when><pairTime><seconds>34.0</seconds><fractionalSeconds>0.21</fractionalSeconds></pairTime></when>' dom = pyxb.utils.domutils.StringToDOM(xml) instance = CreateFromDOM(dom.documentElement) self.assertEqual(instance.sgTime._element(), pairTime) self.assertEqual(instance.sgTime.seconds, 34) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xml) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(xml)) instance = handler.rootObject() self.assertEqual(instance.sgTime._element(), pairTime) self.assertEqual(instance.sgTime.seconds, 34) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xml)
def testComplexInternal(self): xmlt = six.u( '<complex><full>full content</full><optional>optional content</optional></complex>' ) xmld = xmlt.encode('utf-8') doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, 'optional content') self.assertFalse(instance.optional._isNil()) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, 'optional content') self.assertFalse(instance.optional._isNil()) xmlt = six.u( '<complex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><full>full content</full><optional xsi:nil="true"></optional></complex>' ) xmld = xmlt.encode('utf-8') doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, '') self.assertTrue(instance.optional._isNil()) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, '') self.assertTrue(instance.optional._isNil()) xmlt = six.u( '<complex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>' ) xmld = xmlt.encode('utf-8') instance._setIsNil() self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding()
def testComplexInternal (self): xmlt = u'<complex><full>full content</full><optional>optional content</optional></complex>' xmld = xmlt.encode('utf-8') doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, 'optional content') self.assertFalse(instance.optional._isNil()) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, 'optional content') self.assertFalse(instance.optional._isNil()) xmlt = u'<complex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><full>full content</full><optional xsi:nil="true"></optional></complex>' xmld = xmlt.encode('utf-8') doc = pyxb.utils.domutils.StringToDOM(xmlt) instance = CreateFromDOM(doc.documentElement) self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, '') self.assertTrue(instance.optional._isNil()) self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding() saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() saxer.parse(io.StringIO(xmlt)) instance = handler.rootObject() self.assertEqual(instance.full, 'full content') self.assertEqual(instance.optional, '') self.assertTrue(instance.optional._isNil()) xmlt = u'<complex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>' xmld = xmlt.encode('utf-8') instance._setIsNil() self.assertEqual(instance.toDOM().documentElement.toxml("utf-8"), xmld) instance.validateBinding()
def testSGTime (self): xml = '<when><sgTime>2009-06-15T17:50:00Z</sgTime></when>' dom = pyxb.utils.domutils.StringToDOM(xml) self.assertRaises(pyxb.AbstractElementError, CreateFromDOM, dom.documentElement) saxer = pyxb.binding.saxer.make_parser(fallback_namespace=Namespace) handler = saxer.getContentHandler() self.assertRaises(pyxb.AbstractElementError, saxer.parse, StringIO.StringIO(xml)) xml = '<sgTime>2009-06-15T17:50:00Z</sgTime>' dom = pyxb.utils.domutils.StringToDOM(xml) self.assertRaises(pyxb.AbstractElementError, CreateFromDOM, dom.documentElement) self.assertRaises(pyxb.AbstractElementError, saxer.parse, StringIO.StringIO(xml)) xml = '<ISO8601>2009-06-15T17:50:00Z</ISO8601>' dom = pyxb.utils.domutils.StringToDOM(xml) instance = CreateFromDOM(dom.documentElement) self.assertEqual(instance._element(), ISO8601) saxer.parse(StringIO.StringIO(xml)) instance = handler.rootObject() self.assertEqual(instance._element(), ISO8601)
import pyxb.binding.saxer import opc import dml.dml, pml.pml, props.props, sml.sml, wml.wml if len(sys.argv) < 3: print "Usage: xls_sample.py <worklist.py> <input_file> <output_dir>" sys.exit(1) else: exec "import "+sys.argv[1]+" as worklist" inFile = sys.argv[2] (inFileName,inFileExt) = os.path.splitext(os.path.basename(inFile)) outDir = sys.argv[3] package = opc.OPCPackage(inFile) iteration=1 for (fragment, mimetype, schema, reltype) in package.files(worklist.mimetypes): saxer = pyxb.binding.saxer.make_parser(location_base=fragment) handler = saxer.getContentHandler() saxer.parse(StringIO.StringIO(package.read(fragment))) sax_instance = handler.rootObject() for contentIter in sax_instance.iterateBinding(worklist.worklist(mimetype)): # iterate content n times for i in range(worklist.iterations(mimetype)): contentIter() currOutFile = outDir+"/"+inFileName+str(iteration)+inFileExt package.copyWithReplace(currOutFile,{fragment: sax_instance.toxml().encode('utf-8')}) iteration += 1
xml_file = 'ipo.xml' import pyxb.binding.saxer import ipo def ShowOrder(order): print '%s is sending %s %d thing(s):' % (order.billTo().name(), order.shipTo().name(), len(order.items().item())) for item in order.items().item(): print ' Quantity %d of %s at $%s' % ( item.quantity(), item.productName(), item.USPrice()) if False: import pyxb.utils.domutils xmld = pyxb.utils.domutils.StringToDOM(file(xml_file).read()) dom_value = ipo.CreateFromDOM(xmld.documentElement) ShowOrder(dom_value) saxer = pyxb.binding.saxer.make_parser() handler = saxer.getContentHandler() saxer.parse(file(xml_file)) ShowOrder(handler.rootObject())
dom_instance = tmstvd.CreateFromDOM(dom.documentElement) print('minidom first callSign at %s' %(dom_instance.stations.station[0].callSign._location(),)) mt4 = time.time() print('Generating binding from %s with SAXDOM' % (xml_file,)) dt1 = time.time() dom = pyxb.utils.saxdom.parse(io.BytesIO(xmld), location_base=xml_file) dt2 = time.time() #cProfile.run('tmstvd.CreateFromDOM(dom.documentElement)', 'saxdom.prf') saxdom_instance = tmstvd.CreateFromDOM(dom.documentElement) print('SAXDOM first callSign at %s' % (saxdom_instance.stations.station[0].callSign._location(),)) dt3 = time.time() print('Generating binding from %s with SAX' % (xml_file,)) st1 = time.time() saxer = pyxb.binding.saxer.make_parser(location_base=xml_file) handler = saxer.getContentHandler() st2 = time.time() saxer.parse(io.BytesIO(xmld)) #cProfile.run('saxer.parse(open(xml_file))', 'sax.prf') st3 = time.time() sax_instance = handler.rootObject() print('SAXER first callSign at %s' % (sax_instance.stations.station[0].callSign._location(),)) print('DOM-based read %f, parse %f, bind %f, total %f' % (mt2-mt1, mt3-mt2, mt4-mt3, mt4-mt2)) print('SAXDOM-based parse %f, bind %f, total %f' % (dt2-dt1, dt3-dt2, dt3-dt1)) print('SAX-based read %f, parse and bind %f, total %f' % (st2-st1, st3-st2, st3-st1)) print("Equality test on DOM vs SAX: %s" % (dom_instance.equal(sax_instance),)) print("Equality test on SAXDOM vs SAX: %s" % (saxdom_instance.equal(sax_instance, verbose=True),))
from __future__ import print_function xml_file = 'ipo.xml' import pyxb.binding.saxer import ipo def ShowOrder (order): print('%s is sending %s %d thing(s):' % (order.billTo.name, order.shipTo.name, len(order.items.item))) for item in order.items.item: print(' Quantity %d of %s at $%s' % (item.quantity, item.productName, item.USPrice)) if False: import pyxb.utils.domutils xmld = pyxb.utils.domutils.StringToDOM(open(xml_file).read()) dom_value = ipo.CreateFromDOM(xmld.documentElement) ShowOrder(dom_value) saxer = pyxb.binding.saxer.make_parser() handler = saxer.getContentHandler() saxer.parse(open(xml_file)) ShowOrder(handler.rootObject())
print('Generating binding from %s with SAXDOM' % (xml_file, )) dt1 = time.time() dom = pyxb.utils.saxdom.parse(io.BytesIO(xmld), location_base=xml_file) dt2 = time.time() #cProfile.run('tmstvd.CreateFromDOM(dom.documentElement)', 'saxdom.prf') saxdom_instance = tmstvd.CreateFromDOM(dom.documentElement) print('SAXDOM first callSign at %s' % (saxdom_instance.stations.station[0].callSign._location(), )) dt3 = time.time() print('Generating binding from %s with SAX' % (xml_file, )) st1 = time.time() saxer = pyxb.binding.saxer.make_parser(location_base=xml_file) handler = saxer.getContentHandler() st2 = time.time() saxer.parse(io.BytesIO(xmld)) #cProfile.run('saxer.parse(open(xml_file))', 'sax.prf') st3 = time.time() sax_instance = handler.rootObject() print('SAXER first callSign at %s' % (sax_instance.stations.station[0].callSign._location(), )) print('DOM-based read %f, parse %f, bind %f, total %f' % (mt2 - mt1, mt3 - mt2, mt4 - mt3, mt4 - mt2)) print('SAXDOM-based parse %f, bind %f, total %f' % (dt2 - dt1, dt3 - dt2, dt3 - dt1)) print('SAX-based read %f, parse and bind %f, total %f' % (st2 - st1, st3 - st2, st3 - st1)) print("Equality test on DOM vs SAX: %s" % (dom_instance.equal(sax_instance), )) print("Equality test on SAXDOM vs SAX: %s" % (saxdom_instance.equal(sax_instance, verbose=True), ))
from __future__ import print_function xml_file = 'ipo.xml' import pyxb.binding.saxer import ipo def ShowOrder(order): print('%s is sending %s %d thing(s):' % (order.billTo.name, order.shipTo.name, len(order.items.item))) for item in order.items.item: print(' Quantity %d of %s at $%s' % (item.quantity, item.productName, item.USPrice)) if False: import pyxb.utils.domutils xmld = pyxb.utils.domutils.StringToDOM(open(xml_file).read()) dom_value = ipo.CreateFromDOM(xmld.documentElement) ShowOrder(dom_value) saxer = pyxb.binding.saxer.make_parser() handler = saxer.getContentHandler() saxer.parse(open(xml_file)) ShowOrder(handler.rootObject())
xml_file = 'ipo.xml' import pyxb.binding.saxer import ipo def ShowOrder (order): print '%s is sending %s %d thing(s):' % (order.billTo().name(), order.shipTo().name(), len(order.items().item())) for item in order.items().item(): print ' Quantity %d of %s at $%s' % (item.quantity(), item.productName(), item.USPrice()) if False: import pyxb.utils.domutils xmld = pyxb.utils.domutils.StringToDOM(file(xml_file).read()) dom_value = ipo.CreateFromDOM(xmld.documentElement) ShowOrder(dom_value) saxer = pyxb.binding.saxer.make_parser() handler = saxer.getContentHandler() saxer.parse(file(xml_file)) ShowOrder(handler.rootObject())