Code Monkey home page Code Monkey logo

easyxml's Introduction

EasyXML is an easy and concise way to generate XML output in Python.  It uses a
custom attribute getter so element names can be specified directly in the code:

  books = EasyXML('books')
  books.book(title='Example A')
  books.book.author(name='John Smith', age=57)
  books.book.publisher(name='Publisher A')
  books.book(title='Example B')
  books.book.author(name='Jane Doe', age=30)
  books.book.author(name='James Cutter', age=45)
  books.book.publisher(name='Publisher B')
  print str(books)

The above code produces the following XML:

  <books>
    <book title="Example A">
      <author age="57" name="John Smith"/>
      <publisher name="Publisher A"/>
    </book>
    <book title="Example B">
      <author age="30" name="Jane Doe"/>
      <author age="45" name="James Cutter"/>
      <publisher name="Publisher B"/>
    </book>
  </books>

You don't actually need to create every parent element, allowing the following
code to work:

  root = EasyXML('root')
  root.a.b.c()
  root.a.b.c()
  root.a()
  root.a.b.c()
  root.a.b.c()
  print str(root)

The above code produces the following XML:

  <root>
    <a>
      <b>
        <c/>
        <c/>
      </b>
    </a>
    <a>
      <b>
        <c/>
        <c/>
      </b>
    </a>
  </root>

Creating an element returns that element, which can then be passed to helper
methods:

  def material(primitive, ambient, diffuse):
      primitive.ambient(r=ambient[0], g=ambient[1], b=ambient[2])
      primitive.diffuse(r=diffuse[0], g=diffuse[1], b=diffuse[2])

  root = EasyXML('root')
  material(root.primitive(type='sphere'), (64, 0, 0), (192, 0, 0))
  material(root.primitive(type='cube'), (0, 64, 0), (0, 192, 0))
  print str(root)

The above code produces the following XML:

  <root>
    <primitive type="sphere">
      <ambient b="0" g="0" r="64"/>
      <diffuse b="0" g="0" r="192"/>
    </primitive>
    <primitive type="cube">
      <ambient b="0" g="64" r="0"/>
      <diffuse b="0" g="192" r="0"/>
    </primitive>
  </root>

easyxml's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.