Reputation: 13178
I have a xsd
for which I used pyxb
to generate object classes for. So far everything is working, i'm able to take in documents, error handling, etc. is working fine. My only question is this, I have the following in my xsd:
<xs:element name="users">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="user" />
</xs:sequence>
</xs:complexType>
</xs:element>
I have the user
complex type defined elsewhere. Sometimes I want to take a user
from the main xml document and I want to create python class bindings from it...but i'm not able to. pyxb
only allows me to create from an entire document(using the CreateFromDocument
function). Is there anyway to get just that one user
element?
I read the following:
https://developer.yahoo.com/python/python-xml.html
http://pyxb.sourceforge.net/api/pyxb.binding.basis.element-class.html
http://pyxb.sourceforge.net/userref_pyxbgen.html
Upvotes: 0
Views: 1568
Reputation: 13178
So the answer was hidden will into the docs.
http://pyxb.sourceforge.net/PyXB-1.2.2/examples.html
say you have one user
xml document. You can do:
from xml.dom import minidom
dom = minidom.parseString(string)
# import the xml file you get from pyxbgen
import user_xsd
user_xsd.CreateFromDOM(dom.documentElement)
Upvotes: 0
Reputation: 989
CreateFromDocument() will create a binding for any XML fragment that is a top-level element in the schema. So you should have been able to do:
instance = user_xsd.CreateFromDocument(string)
without going through a dom instance first. There are a lot of examples that do exactly this in the tests directory.
Upvotes: 1