ANN: XML builder for Python

J

Jonas Galvez

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
 
W

Walter Dörwald

Stefan said:
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
 
J

Jonas Galvez

Walter said:
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
 
S

Stefan Behnel

Hi,
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
 
J

Jonas Galvez

Stefan said:
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
 
W

Walter Dörwald

Stefan said:
Hi,


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
 
W

Walter Dörwald

Jonas said:
Walter said:
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
 
J

Jonas Galvez

Walter said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top