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:

mnservices[4]$ pyxbgen \
  --wsdl-location="http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl" --module=weather \
  --write-for-customization
urn:uuid:57d386ce-bb48-11de-a28f-001cc05930fc
Retrieving WSDL from http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl
Importing pyxb.binding.datatypes to get binding for wildcard {http://www.w3.org/2001/XMLSchema}schema
NOTE: Created unbound wildcard element from DOM node {http://www.w3.org/2001/XMLSchema}schema
PS urn:uuid:57d386ce-bb48-11de-a28f-001cc05930fc
Python for http://ws.cdyne.com/WeatherWS/ requires 1 modules
Saved binding source to ./raw/weather.py
mnservices[5]$

Then write a program that uses them:

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

uri = 'http://ws.cdyne.com/WeatherWS/Weather.asmx/GetCityForecastByZIP?ZIP=85711'
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:

mnservices[5]$ python client_get.py
Weather forecast for Tucson, AZ:
  Saturday, October 17 2009: Sunny, from  to 98
  Sunday, October 18 2009: Partly Cloudy, from 68 to 96
  Monday, October 19 2009: Partly Cloudy, from 67 to 91
  Tuesday, October 20 2009: Sunny, from 65 to 88
  Wednesday, October 21 2009: Sunny, from 59 to 86
  Thursday, October 22 2009: Sunny, from 58 to 87
  Friday, October 23 2009: Sunny, from 60 to 8
mnservices[6]$

That’s it.

Indices and tables

Table Of Contents

Next topic

Overview

This Page