Scratch notes from Amara/Whatsnew/Scratch

Generating XML

The API for writing XML, HTML, text, etc. directly (i.e. not through methods on node objects) has been greatly improved.

from amara import writer
w = writer(indent=u"yes") #Operates in streaming mode
w.start_document()
w.start_element(u'xsa')
w.start_element(u'vendor')
#Element with simple text (#PCDATA) content
w.simple_element(u'name', content=u'Centigrade systems')
#Note writer.text(content) still works
w.simple_element(u'email', content=u"info@centigrade.bogus")
w.end_element(u'vendor')
#Element with an attribute
w.start_element(u'product', attributes={u'id': u"100\u00B0"})
#Note w.attribute(name, value, namespace=None) still works
w.simple_element(u'name', content=u"100\u00B0 Server")
#XML fragment
w.xml_fragment('<version>1.0</version><last-release>20030401</last-release>')
#Empty element
w.simple_element(u'changes')
w.end_element(u'product')
w.end_element(u'xsa')
w.end_document()

By default it will operate in XML mode.

The following are the supported output parameters:

method is important because it controls the fundamental nature of the output. To generate HTML, do:

w = writer(method="html")

The supported methods:

* xml * html * xhtml * text

For c14n, use it as follows:

w = writer(method="xml", canonical_form=True) #Technically you don't need the method=.  canonical_form forces XML

Note: if you use c14n the encoding is forced to UTF-8, indent is forced to False, byte-order-mark to false, and omit-xml-declaration. If version is given and != 1.0, you'll get an error.

There is a new class of error called WriterError.

tidying html

python -m amara.bindery.html -p http://wiki.xml3k.org/

Amara/Whatsnew/Scratch (last edited 2010-12-03 17:56:26 by LuisMiguel)