GithubHelp home page GithubHelp logo

muttonchop / dexml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rfk/dexml

0.0 1.0 0.0 150 KB

a dead-simple object-xml mapper for python

Home Page: http://github.com/rfk/dexml

License: MIT License

Python 67.38% HTML 2.33% CSS 30.29%

dexml's Introduction

dexml: a dead-simple Object-XML mapper for Python

Let's face it: xml is a fact of modern life. I'd even go so far as to say that it's good at what is does. But that doesn't mean it's easy to work with and it doesn't mean that we have to like it. Most of the time, XML just needs to get out of the way and let you do some actual work instead of writing code to traverse and manipulate yet another DOM.

The dexml module takes the obvious mapping between XML tags and Python objects and lets you capture that as cleanly as possible. Loosely inspired by Django's ORM, you write simple class definitions to define the expected structure of your XML document. Like so:

>>> import dexml
>>> from dexml import fields
>>> class Person(dexml.Model):
...   name = fields.String()
...   age = fields.Integer(tagname='age')

Then you can parse an XML document into an object like this:

>>> p = Person.parse("<Person name='Foo McBar'><age>42</age></Person>")
>>> p.name
u'Foo McBar'
>>> p.age
42

And you can render an object into an XML document like this:

>>> p = Person(name="Handsome B. Wonderful",age=36)
>>> p.render()
'<?xml version="1.0" ?><Person name="Handsome B. Wonderful"><age>36</age></Person>'

Malformed documents will raise a ParseError:

>>> p = Person.parse("<Person><age>92</age></Person>")
Traceback (most recent call last):
    ...
ParseError: required field not found: 'name'

Of course, it gets more interesting when you nest Model definitions, like this:

>>> class Group(dexml.Model):
...   name = fields.String(attrname="name")
...   members = fields.List(Person)
...
>>> g = Group(name="Monty Python")
>>> g.members.append(Person(name="John Cleese",age=69))
>>> g.members.append(Person(name="Terry Jones",age=67))
>>> g.render(fragment=True)
'<Group name="Monty Python"><Person name="John Cleese"><age>69</age></Person><Person name="Terry Jones"><age>67</age></Person></Group>'

There's support for XML namespaces, default field values, case-insensitive parsing, and more fun stuff. Check out the documentation on the following classes for more details:

Model:the base class for objects that map into XML
Field:the base class for individual model fields
Meta:meta-information about how to parse/render a model

dexml's People

Contributors

iurisilvio avatar rfk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.