pythonic way to create xml/(x)html files. Updated version with fixes, unicode support and cleaned API.
Project description
XMLBuilder is tiny library build on top of ElementTree.TreeBuilder to
make xml files creation more pythonomic. `XMLBuilder` use `with`
statement and attribute access to define xml document structure.
Only 2.5+ python versions are supported.
from __future__ import with_statement # only for python 2.5
from xmlbuilder import XMLBuilder
x = XMLBuilder('root')
x.some_tag
x.some_tag_with_data('text', a='12')
with x.some_tree(a='1'):
with x.data:
x.mmm
for i in range(10):
x.node(val=str(i))
etree_node = ~x # <= return xml.etree.ElementTree object
print str(x) # <= string object
will result:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<some_tag />
<some_tag_with_data a="12">text</some_tag_with_data>
<some_tree a="1">
<data>
<mmm />
<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>
</some_tree>
</root>
There some fields, which allow xml output customization:
formatted = produce formatted xml. default = True
tabstep = tab string, used for formatting. default = ' ' * 4
encoding = xml document encoding. default = 'utf-8'
xml_header = add xml header
(<?xml version="1.0" encoding="$DOCUMENT_ENCODING$">)
to begining of the document. default = True
builder = builder class, used for create dcument. Default =
xml.etree.ElementTree.TreeBuilder
Options can be readed by
x = XMLBuilder('root')
print x[option_name]
and changed by
x[option_name] = new_val
Look at xmlbuilder/test.py for UT and more examples.
Happy xml'ing.
make xml files creation more pythonomic. `XMLBuilder` use `with`
statement and attribute access to define xml document structure.
Only 2.5+ python versions are supported.
from __future__ import with_statement # only for python 2.5
from xmlbuilder import XMLBuilder
x = XMLBuilder('root')
x.some_tag
x.some_tag_with_data('text', a='12')
with x.some_tree(a='1'):
with x.data:
x.mmm
for i in range(10):
x.node(val=str(i))
etree_node = ~x # <= return xml.etree.ElementTree object
print str(x) # <= string object
will result:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<some_tag />
<some_tag_with_data a="12">text</some_tag_with_data>
<some_tree a="1">
<data>
<mmm />
<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>
</some_tree>
</root>
There some fields, which allow xml output customization:
formatted = produce formatted xml. default = True
tabstep = tab string, used for formatting. default = ' ' * 4
encoding = xml document encoding. default = 'utf-8'
xml_header = add xml header
(<?xml version="1.0" encoding="$DOCUMENT_ENCODING$">)
to begining of the document. default = True
builder = builder class, used for create dcument. Default =
xml.etree.ElementTree.TreeBuilder
Options can be readed by
x = XMLBuilder('root')
print x[option_name]
and changed by
x[option_name] = new_val
Look at xmlbuilder/test.py for UT and more examples.
Happy xml'ing.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
xmlbuilder-1.0.tar.gz
(4.3 kB
view details)
File details
Details for the file xmlbuilder-1.0.tar.gz
.
File metadata
- Download URL: xmlbuilder-1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdeb3231bfe9caa87abeff64f9eb96c83779eb1748300ac451a37ae4e371a25c |
|
MD5 | 17596b2b8596a17abc8ab56085921b67 |
|
BLAKE2b-256 | ab507d5482347973e449432c7a09385a5d6769db63c95ef98590caa82d0792ae |