PyXB: Python XML Schema Bindings

PyXB (“pixbee”) is a pure Python package that generates Python source code for classes that correspond to data structures defined by XMLSchema. The generated classes support bi-directional conversion between XML documents and Python objects. In concept it is similar to JAXB for Java and CodeSynthesis XSD for C++. A Thirty Second Example is at the bottom of this page. Step-by-step examples are in User Reference.

Getting Help

PyXB is distributed on SourceForge.

For support, consult the Help Forum, or subscribe to and email the mailing list.

To file a bug report or see the status of defects reported against the current release, visit the Trac database.

For a history of releases, see Release History.

Thirty Second Example

An example of a program using PyXB to interact with a web service using an automatically-generated module. First, retrieve the WSDL and generate the bindings:

llc[837]$  pyxbgen \
      --wsdl-location= \
      --module=weather \
Retrieving WSDL from
WARNING:pyxb.binding.basis:Unable to convert DOM node {}schema at Weather.asmx?WSDL[4:4] to binding
PS urn:uuid:029825d0-a6a3-11e2-a410-c8600024e903
WARNING:pyxb.binding.generate:Complex type {}ArrayOfWeatherDescription renamed to ArrayOfWeatherDescription_
WARNING:pyxb.binding.generate:Complex type {}ForecastReturn renamed to ForecastReturn_
WARNING:pyxb.binding.generate:Complex type {}WeatherReturn renamed to WeatherReturn_
Python for requires 1 modules

Then write a program that uses them:

import time
import urllib2
import weather   # Bindings generated by PyXB
import pyxb.utils.domutils as domutils

uri = ''
xml = urllib2.urlopen(uri).read()
doc = domutils.StringToDOM(xml)
fc_return = weather.CreateFromDOM(doc.documentElement)
if fc_return.Success:
    print 'Weather forecast for %s, %s:' % (fc_return.City, fc_return.State)
    for fc in fc_return.ForecastResult.Forecast:
        when = time.strftime('%A, %B %d %Y', fc.Date.timetuple())
        outlook = fc.Desciption # typos in WSDL left unchanged
        low = fc.Temperatures.MorningLow
        high = fc.Temperatures.DaytimeHigh
        print '  %s: %s, from %s to %s' % (when, outlook, low, high)

And run it:

llc[838]$ python
Weather forecast for Saint Paul, MN:
 Friday, March 22 2013: Partly Cloudy, from 8 to 31
 Saturday, March 23 2013: Partly Cloudy, from 17 to 34
 Sunday, March 24 2013: Mostly Cloudy, from 21 to 34
 Monday, March 25 2013: Partly Cloudy, from 21 to 33
 Tuesday, March 26 2013: Partly Cloudy, from 21 to 34
 Wednesday, March 27 2013: Partly Cloudy, from 19 to 36
 Thursday, March 28 2013: Partly Cloudy, from 20 to 36

That’s it.

Indices and tables

Table Of Contents

Next topic


This Page