I am having a hard time getting started with PyXB.
Say I have an XSD file (an XML schema). I would like to:
How can I do this with PyXB? Below is a simple example of an XSD file (from Wikipedia) that encodes an address, but I am having a hard time even getting started.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="FullName" type="xs:string" />
<xs:element name="House" type="xs:string" />
<xs:element name="Street" type="xs:string" />
<xs:element name="Town" type="xs:string" />
<xs:element name="County" type="xs:string" minOccurs="0" />
<xs:element name="PostCode" type="xs:string" />
<xs:element name="Country" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="IN" />
<xs:enumeration value="DE" />
<xs:enumeration value="ES" />
<xs:enumeration value="UK" />
<xs:enumeration value="US" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Once I run
pyxbgen -u example.xsd -m example
I get a example.py
that has the following classes:
example.Address example.STD_ANON
example.CTD_ANON example.StringIO
example.CreateFromDOM example.pyxb
example.CreateFromDocument example.sys
example.Namespace
I think I understand what CreateFromDocument
does - it presumably reads an XML and creates the corresponding python object-, but which class do I use to create a new object and then save it to an XML?
Follow the steps in How to: Create and edit an XSD schema file. In the XML Schema Explorer, right-click the PurchaseOrder global element, and then select Generate Sample XML. When you select this option, the PurchaseOrder. xml file with the following sample XML content will be generated and opened in the XML editor:
A separated file is created for XSD and this document is linked with the respective XML document. With XML Schema Specification a method is specified which is to be included in the root of the XML Document. Here the first statement is the Namespace Declaration.
XML XSD | How XSD works in XML with Examples? XML Schema Definition is defined as a language to describe the structure of the XML document. It means that all the XML Standards are defined in the XSD and they are written in XML. It is recommended by W3C to replace the Document Type Definition (DTD). The schema defines their types or built-in types.
It means that all the XML Standards are defined in the XSD and they are written in XML. It is recommended by W3C to replace the Document Type Definition (DTD). The schema defines their types or built-in types. XML Schema is also allowed to check whether a given XML document is valid with syntactic Confirmation.
A simple google search brings this: http://pyxb.sourceforge.net/userref_pyxbgen.html#pyxbgen
In particular the part that says:
Translate this into Python with the following command:
pyxbgen -u po1.xsd -m po1
The -u parameter identifies a schema document describing contents of a namespace. The parameter may be a path to a file on the local system, or a URL to a network-accessible location like http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd. The -m parameter specifies the name to be used by the Python module holding the bindings generated for the namespace in the preceding schema. After running this, the Python bindings will be in a file named po1.py.
EDIT Following your update:
Now that you have your generated Address
class and all the associated helpers, look at http://pyxb.sourceforge.net/userref_usebind.html in order to learn how to use them. For your specific question, you want to study the "Creating Instances in Python Code" paragraph. Basically to generate XML from your application data you simply do:
import example
address = Address()
address.FullName = "Jo La Banane"
# fill other members of address
# ...
with open('myoutput.xml', 'w') as file
f.write(address.toxml("utf-8"))
Now it's up to you to be curious and read the code being generated, pyxb's doc, call the various generated methods and experiment!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With