Skip to main content

Pythonic way to create xml files

Project description

Example of usage:
-----------------


from __future__ import with_statement
from xmlbuilder import XMLBuilder
x = XMLBuilder(format=True)
with x.root(a = 1):
with x.data:
[x << ('node',{'val':i}) for i in range(10)]

print str(x)

will print

<root a="1">
<data>
<node val="0" />
<node val="1" />
<node val="2" />
<node val="3" />
<node val="4" />
<node val="5" />
<node val="6" />
<node val="7" />
<node val="8" />
<node val="9" />
</data>
</root>

Mercurial repo:http://hg.assembla.com/MyPackages/

Documentations
--------------
`XMLBuilder` is simple library build on top of `ElementTree.TreeBuilder` to
simplify xml files creation as much as possible. Althow it can produce
structured result with identated child tags. `XMLBuilder` use python `with`
statement to define xml tag levels and `<<` operator for simple cases -
text and tag without childs.

First we need to create xmlbuilder

from xmlbuilder import XMLBuilder
# params - encoding = 'utf8',
# builder = None, - ElementTree.TreeBuilder
# tab_level = None, - current tab l;evel - for formatted output only
# format = False, - create formatted output
# tab_step = " " * 4 - indentation step
xml = XMLBuilder()


Use `with` statement to make document structure
#create and open tag 'root_tag' with text 'text' and attributes
with xml.root_tag(text,attr1=val1,attr2=val2):
#create and open tag 'sub_tag'
with xml.sub_tag(text,attr3=val3):
#create tag which are not valid python identificator
with xml('one-more-sub-tag',attr7=val37):
xml << "Some textual data"
#here tag 'one-more-sub-tag' are closed
#Tags without children can be created using `<<` operator
for val in range(15):
xml << ('message',"python rocks!"[:i])
#create 15 child tag like <message> python r</message>
#all tags closed
node = ~x # get etree.ElementTree object
xml_data = str(x)
unicode_xml_data = unicode(x)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlbuilder-0.9.zip (9.1 kB view details)

Uploaded Source

Built Distribution

xmlbuilder-0.9-py2.6.egg (9.0 kB view details)

Uploaded Egg

File details

Details for the file xmlbuilder-0.9.zip.

File metadata

  • Download URL: xmlbuilder-0.9.zip
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xmlbuilder-0.9.zip
Algorithm Hash digest
SHA256 3fec4009e0254e8bf4735bc3f6814e0dab7baaf656fc92cc4f73820a7a676a78
MD5 7cda33d9228bbfe19c273660ff7e1437
BLAKE2b-256 079cc9752daa264ea345608ce87614967c91315c49ca2687a0d01df235828a26

See more details on using hashes here.

File details

Details for the file xmlbuilder-0.9-py2.6.egg.

File metadata

  • Download URL: xmlbuilder-0.9-py2.6.egg
  • Upload date:
  • Size: 9.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xmlbuilder-0.9-py2.6.egg
Algorithm Hash digest
SHA256 a2b62c9fd27abff5125b88d24554bc2d457c985602110b13cb44da74069db7ef
MD5 ebce241baadf6eb8b800a1133acea519
BLAKE2b-256 96a4352112ed11ed0ba0a246b9180db2c311f3b42f8e672836fff431ef3d2ae7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page