ANN: XML builder for Python

Discussion in 'Python' started by Jonas Galvez, Jul 3, 2008.

  1. Jonas Galvez

    Jonas Galvez Guest

    Not sure if it's been done before, but still...

    from __future__ import with_statement
    from xmlbuilder import builder, element

    xml = builder(version="1.0", encoding="utf-8")
    with xml.feed(xmlns='http://www.w3.org/2005/Atom'):
    xml.title('Example Feed')
    xml.link(None, href='http://example.org/')
    xml.updated('2003-12-13T18:30:02Z')
    with xml.author:
    xml.name('John Doe')
    xml.id('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6')
    with xml.entry:
    xml.title('Atom-Powered Robots Run Amok')
    xml.link(None, href='http://example.org/2003/12/13/atom03')
    xml.id('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a')
    xml.updated('2003-12-13T18:30:02Z')
    xml.summary('Some text.')
    print xml

    Will produce:

    <?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
    <title>Example Feed</title>
    <link href="http://example.org/" />
    <updated>2003-12-13T18:30:02Z</updated>
    <author>
    <name>John Doe</name>
    </author>
    <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
    <entry>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="http://example.org/2003/12/13/atom03" />
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
    </entry>
    </feed>

    http://github.com/galvez/gae-rest/tree/258066f5e1a32c999e04a9313943fdfa8e64edd9/xmlbuilder.py

    --Jonas Galvez
    Jonas Galvez, Jul 3, 2008
    #1
    1. Advertising

  2. Stefan Behnel, Jul 3, 2008
    #2
    1. Advertising

  3. Stefan Behnel wrote:
    > Jonas Galvez wrote:
    >> Not sure if it's been done before, but still...

    >
    > Obviously ;)
    >
    > http://codespeak.net/lxml/tutorial.html#the-e-factory
    >
    > ... and tons of other tools that generate XML, check PyPI.


    Although it might be the first time I see the with statement "misused" for
    this. :)

    Stefan
    Stefan Behnel, Jul 3, 2008
    #3
  4. Stefan Behnel wrote:
    > Stefan Behnel wrote:
    >> Jonas Galvez wrote:
    >>> Not sure if it's been done before, but still...

    >> Obviously ;)
    >>
    >> http://codespeak.net/lxml/tutorial.html#the-e-factory
    >>
    >> ... and tons of other tools that generate XML, check PyPI.

    >
    > Although it might be the first time I see the with statement "misused" for
    > this. :)


    XIST has been using with blocks since version 3.0.

    Take a look at:
    http://www.livinglogic.de/Python/xist/Examples.html


    from __future__ import with_statement

    from ll.xist import xsc
    from ll.xist.ns import html, xml, meta

    with xsc.Frag() as node:
    +xml.XML()
    +html.DocTypeXHTML10transitional()
    with html.html():
    with html.head():
    +meta.contenttype()
    +html.title("Example page")
    with html.body():
    +html.h1("Welcome to the example page")
    with html.p():
    +xsc.Text("This example page has a link to the ")
    +html.a("Python home page", href="http://www.python.org/")
    +xsc.Text(".")

    print node.conv().bytes(encoding="us-ascii")

    Servus,
    Walter
    Walter Dörwald, Jul 3, 2008
    #4
  5. Jonas Galvez

    Jonas Galvez Guest

    Walter Dörwald wrote:
    > XIST has been using with blocks since version 3.0.
    > [...]
    > with xsc.Frag() as node:
    > +xml.XML()
    > +html.DocTypeXHTML10transitional()
    > with html.html():
    > [...]


    Sweet! I don't like having to use the unary operator tho, I wanted
    something as simple as possible, so I wouldn't even have to assign a
    variable on the with block ("as something"). I plan to add some
    validation and error checking, but for generating feeds for my Atom
    store it's reasonably fast and lean (just over 50 lines of code).

    --Jonas Galvez, http://jonasgalvez.com.br/log
    Jonas Galvez, Jul 3, 2008
    #5
  6. Hi,

    Walter Dörwald wrote:
    > XIST has been using with blocks since version 3.0.
    >
    > Take a look at:
    > http://www.livinglogic.de/Python/xist/Examples.html
    >
    >
    > from __future__ import with_statement
    >
    > from ll.xist import xsc
    > from ll.xist.ns import html, xml, meta
    >
    > with xsc.Frag() as node:
    > +xml.XML()
    > +html.DocTypeXHTML10transitional()
    > with html.html():
    > with html.head():
    > +meta.contenttype()
    > +html.title("Example page")
    > with html.body():
    > +html.h1("Welcome to the example page")
    > with html.p():
    > +xsc.Text("This example page has a link to the ")
    > +html.a("Python home page", href="http://www.python.org/")
    > +xsc.Text(".")
    >
    > print node.conv().bytes(encoding="us-ascii")


    Interesting. Is the "+" actually required? Are there other operators that make
    sense here? I do not see what "~" or "-" could mean.

    Or is it just a technical constraint?

    I'm asking because I consider adding such a syntax to lxml as a separate
    module. And I'd prefer copying an existing syntax over a (badly) home grown one.

    Stefan
    Stefan Behnel, Jul 3, 2008
    #6
  7. Jonas Galvez

    Jonas Galvez Guest

    Stefan Behnel wrote:
    > Interesting. Is the "+" actually required? Are there other operators
    > that make sense here? I do not see what "~" or "-" could mean.
    > Or is it just a technical constraint?
    > I'm asking because I consider adding such a syntax to lxml as
    > a separate module. And I'd prefer copying an existing syntax over
    > a (badly) home grown one.


    I believe it's related to something tricky about the with statement.

    You have to be able to call the "element builder" both in the with statement:

    with xml.element ...

    And also inside the block itself:

    with xml.element ...:
    xml.otherelement()

    Except the with element expects an object that implements __enter__()
    and __exit__().

    The solution I found to this was to make __getattr__() return a
    ready-to-use object that implements those methods while also taking
    keyword parameters but *not* text. So for instance:

    with xml.element(foo=1):
    xml.foo("bar")

    Will work (<element foo=1><foo>bar</foo></element>) but:

    with xml.element("text", foo=1):
    xml.foo("bar")

    Will not. Also, in inline calls to the builder you have to explictly
    set the element-content (value) parameter, even if it's simply None,
    so __call__() can identify that you're (supposedly) calling it inline
    and not with the 'with' statement.

    That is an acceptable tradeoff, however, considering you would rarely
    append text nodes right after an element while also having other child
    elements. I believe my xmlbuilder will work well for Atom, XML-RPC,
    SOAP etc, but definitely not for HTML! :)

    Overriding + is a rather clever way to prevent this (so you can
    actually know you're calling it inline), but I don't really see a need
    for it for most things XML.

    --Jonas Galvez, http://jonasgalvez.com.br/log
    Jonas Galvez, Jul 3, 2008
    #7
  8. Stefan Behnel wrote:
    > Hi,
    >
    > Walter Dörwald wrote:
    >> XIST has been using with blocks since version 3.0.
    >>
    >> Take a look at:
    >> http://www.livinglogic.de/Python/xist/Examples.html
    >>
    >>
    >> from __future__ import with_statement
    >>
    >> from ll.xist import xsc
    >> from ll.xist.ns import html, xml, meta
    >>
    >> with xsc.Frag() as node:
    >> +xml.XML()
    >> +html.DocTypeXHTML10transitional()
    >> with html.html():
    >> with html.head():
    >> +meta.contenttype()
    >> +html.title("Example page")
    >> with html.body():
    >> +html.h1("Welcome to the example page")
    >> with html.p():
    >> +xsc.Text("This example page has a link to the ")
    >> +html.a("Python home page", href="http://www.python.org/")
    >> +xsc.Text(".")
    >>
    >> print node.conv().bytes(encoding="us-ascii")

    >
    > Interesting. Is the "+" actually required? Are there other operators that make
    > sense here? I do not see what "~" or "-" could mean.


    Of course the node constructor could append the node to the currently
    active element. However there might be cases where you want to do
    something else with the newly created node, so always appending the node
    is IMHO the wrong thing.

    > Are there other operators that make
    > sense here? I do not see what "~" or "-" could mean.
    >
    > Or is it just a technical constraint?


    You need *one* operator/method that appends a node to the currently
    active block without opening another new block. This operator should be
    short to type and should have the right connotations. I find that unary
    + is perfect for that.

    > I'm asking because I consider adding such a syntax to lxml as a separate
    > module. And I'd prefer copying an existing syntax over a (badly) home

    grown one.

    "Existing syntax" might be a little exaggeration, I know of no other
    Python package that uses __pos__ for something similar. (But then again,
    I know of no other Python package that uses with block for generating
    XML ;)).

    Servus,
    Walter
    Walter Dörwald, Jul 3, 2008
    #8
  9. Jonas Galvez wrote:
    > Walter Dörwald wrote:
    >> XIST has been using with blocks since version 3.0.
    >> [...]
    >> with xsc.Frag() as node:
    >> +xml.XML()
    >> +html.DocTypeXHTML10transitional()
    >> with html.html():
    >> [...]

    >
    > Sweet! I don't like having to use the unary operator tho, I wanted
    > something as simple as possible, so I wouldn't even have to assign a
    > variable on the with block ("as something").


    You only have to assign the node a name in the outermost with block so
    that you can use the node object afterwards. But of course you can
    always implement the outermost __enter__/__exit__ in such a way, that
    the node gets written to an outputstream immediately.

    > I plan to add some
    > validation and error checking, but for generating feeds for my Atom
    > store it's reasonably fast and lean (just over 50 lines of code).


    Servus,
    Walter
    Walter Dörwald, Jul 3, 2008
    #9
  10. Jonas Galvez

    Jonas Galvez Guest

    Walter Dörwald wrote:
    > Of course the node constructor could append the
    > node to the currently active element. However there
    > might be cases where you want to do something else
    > with the newly created node, so always appending the
    > node is IMHO the wrong thing.


    Unless you're using it as a templating engine of sorts, much like
    Ruby's builder used in Rails.

    Which is exactly what I'm trying to get at. I don't want to store the
    tree in any way or be able to modify it afterwards, I just want to
    generate the XML as the block is executed. Using ElementTree as
    suggested previously might be a good solution if you want to preserve
    what you've generated, tho.

    --Jonas Galvez
    Jonas Galvez, Jul 4, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    507
  2. Lothar Werzinger
    Replies:
    1
    Views:
    499
    fuzzylollipop
    Dec 27, 2004
  3. Replies:
    0
    Views:
    353
  4. Kevin Ollivier

    [ANN] EClass.Builder 2.5.4.17

    Kevin Ollivier, Aug 25, 2004, in forum: Python
    Replies:
    0
    Views:
    268
    Kevin Ollivier
    Aug 25, 2004
  5. Phlip
    Replies:
    5
    Views:
    545
    Stefan Behnel
    Jan 13, 2010
Loading...

Share This Page