5 from __future__ import with_statement
\r
6 from xmlbuilder import XMLBuilder
\r
7 x = XMLBuilder(format=True)
\r
10 [x << ('node',{'val':i}) for i in range(10)]
\r
31 Mercurial repo:http://hg.assembla.com/MyPackages/
\r
35 `XMLBuilder` is simple library build on top of `ElementTree.TreeBuilder` to
\r
36 simplify xml files creation as much as possible. Althow it can produce
\r
37 structured result with identated child tags. `XMLBuilder` use python `with`
\r
38 statement to define xml tag levels and `<<` operator for simple cases -
\r
39 text and tag without childs.
\r
41 First we need to create xmlbuilder
\r
43 from xmlbuilder import XMLBuilder
\r
44 # params - encoding = 'utf8',
\r
45 # builder = None, - ElementTree.TreeBuilder
\r
46 # tab_level = None, - current tab l;evel - for formatted output only
\r
47 # format = False, - create formatted output
\r
48 # tab_step = " " * 4 - indentation step
\r
52 Use `with` statement to make document structure
\r
53 #create and open tag 'root_tag' with text 'text' and attributes
\r
54 with xml.root_tag(text,attr1=val1,attr2=val2):
\r
55 #create and open tag 'sub_tag'
\r
56 with xml.sub_tag(text,attr3=val3):
\r
57 #create tag which are not valid python identificator
\r
58 with xml('one-more-sub-tag',attr7=val37):
\r
59 xml << "Some textual data"
\r
60 #here tag 'one-more-sub-tag' are closed
\r
61 #Tags without children can be created using `<<` operator
\r
62 for val in range(15):
\r
63 xml << ('message',"python rocks!"[:i])
\r
64 #create 15 child tag like <message> python r</message>
\r
66 node = ~x # get etree.ElementTree object
\r
68 unicode_xml_data = unicode(x)
\r