Below is an example with several approaches that work for me. I've also added some comments in context explaining why what you tried didn't work.
import items inv = items.items() # can't have empty inventory; need minOccurs="0" #print inv.toxml() i1 = items.Item(name="cheese", price="50") inv.item.append(i1) print type(inv) i2 = items.Item(name="parrot", price="200") inv.item.append(i2) print(inv.toxml()) inv.item[:] = [i2, i1] print(inv.toxml()) inv = items.items(i1, i2) print(inv.toxml()) On Wed, Apr 16, 2014 at 7:04 AM, Serge Gadioux <serge.gadi...@gmail.com> wrote: > I also tried to use the append method() : > > inv = items.Items() The error here is you're creating an instance of the type Items, not the element items. > inv.item.append(i1) > inv.item.append(i2) > print(inv.toxml()) > > This time, the error comes from toxml() : > > Traceback (most recent call last): > File "test2.py", line 9, in <module> > print(inv.toxml()) > File "C:\Documents and Settings\sgadioux\Application > Data\Python\Python27\site-packages\pyxb\binding\basis.py", line 539, > in toxml > dom = self.toDOM(bds) > File "C:\Documents and Settings\sgadioux\Application > Data\Python\Python27\site-packages\pyxb\binding\basis.py", line 511, > in toDOM > raise pyxb.UnboundElementError(self) > pyxb.exceptions_.UnboundElementError: Instance of type Items has no > bound element for start tag > > > What is the right way to build such a list of items from Python ? > > Thanks, > Serge > > > On Wed, Apr 16, 2014 at 1:46 PM, Serge Gadioux <serge.gadi...@gmail.com> > wrote: >> Hi, >> >> I am trying to use PyXB (1.2.3) to produce XML from python object. >> I have a simple xsd schema: >> >> <?xml version="1.0" encoding="UTF-8"?> >> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> >> >> <xsd:complexType name="Items"> >> <xsd:sequence> >> <xsd:element name="item" type="Item" maxOccurs="unbounded"/> >> </xsd:sequence> >> </xsd:complexType> >> >> <xsd:complexType name="Item"> >> <xsd:attribute name="name" type="xsd:string" use="required"/> >> <xsd:attribute name="price" type="xsd:string" use="required"/> >> </xsd:complexType> >> >> <xsd:element name="items" type="Items"/> >> >> </xsd:schema> >> >> >> I generated bindings with the command: >> pyxbgen" -u items.xsd -m items >> >> I am able to read an xml (with CreateFromDocument) and get an Items object. >> >> Then if try to build the document from python to generate XML, I get an >> error. >> I use the following script: >> >> import items >> >> i1 = items.Item(name="cheese", price="50") >> i2 = items.Item(name="parrot", price="200") >> inv = items.Items([i1, i2]) The problem here is that the content you've provided is a Python list, not a sequence of element bindings. >> print(inv.tomxl()) >> >> Running this script gives me pyxb.exceptions_.MixedContentError: Invalid >> non-element content >> Here is the stack trace: >> Traceback (most recent call last): >> File "test2.py", line 5, in <module> >> inv = items.Items([i1, i2]) >> File "C:\Documents and Settings\sgadioux\Application >> Data\Python\Python27\site-packages\pyxb\binding\basis.py", line 2060, in >> __init__ >> self.extend(args, _from_xml=from_xml, _location=location) >> File "C:\Documents and Settings\sgadioux\Application >> Data\Python\Python27\site-packages\pyxb\binding\basis.py", line 2517, in >> extend >> [ self.append(_v, _fallback_namespace=_fallback_namespace, >> _from_xml=_from_xml, _location=_location) for _v in value_list ] >> File "C:\Documents and Settings\sgadioux\Application >> Data\Python\Python27\site-packages\pyxb\binding\basis.py", line 2505, in >> append >> raise pyxb.MixedContentError(self, value, location) >> pyxb.exceptions_.MixedContentError: Invalid non-element content >> >> What am I doing wrong ? Do I need to specify a namespace in my xsd ? >> >> Thanks in advance, >> Serge > > ------------------------------------------------------------------------------ > Learn Graph Databases - Download FREE O'Reilly Book > "Graph Databases" is the definitive new guide to graph databases and their > applications. Written by three acclaimed leaders in the field, > this first edition is now available. Download your free book today! > http://p.sf.net/sfu/NeoTech > _______________________________________________ > pyxb-users mailing list > pyxb-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/pyxb-users ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/NeoTech _______________________________________________ pyxb-users mailing list pyxb-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pyxb-users