xmlindent (version 1.2) | index /home/pjk/websrc/xmlindent.py |
Generate indented XML.
An extension of the XMLGenerator class from the SAX interface which provides
features for generating indented XML. The method 'newline' in the extension
class XMLIndentGenerator outputs a newline character and the appropriate
number of blank characters for indentation of the next item.
Other features are:
- Attributes to the 'startElement' method are passed as keyword arguments.
- The extension class maintains an internal stack of current element tags,
obviating the need to specify tag name in method 'endElement'. This
simplifies XML generation slightly for application code.
- A number of common attribute keys are automatically translated from
the form 'xxx_yyy' to 'xxx:yyy', to simplify giving attributes
as keyword arguments.
- The class 'Xml' to build lightweight element trees for output with
XMLIndentGenerator.
The prefix mapping and NS methods have not yet been properly implemented.
Example usage:
from xmlindent import XMLIndentGenerator
gen = XMLIndentGenerator()
gen.startDocument()
gen.startElement('root', attr1='1', attr2='text&data')
gen.newline()
gen.startElement('item')
xml = Xml('another', Xml('and-another'), id='2', color='grey')
xml.output(gen)
gen.newline()
gen.startElement('item2')
gen.characters('stuff')
gen.endElement()
gen.newline()
gen.endElement()
gen.newline()
gen.endElement()
gen.endDocument()
This is the resulting XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<root attr2="text&data" attr1="1">
<item>
<another color="grey" id="2">
<and-another/>
</another>
<item2>stuff</item2>
</item>
</root>
------------------------------------------------------------
Version 1.2
2004-12-09 split out from other code
2007-08-23 cleanup, docs written
2007-08-31 added Xml
Copyright (C) 2007 Per Kraulis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Classes | ||||||||||||||||||
|
Data | ||
__version__ = '1.2' |