XMLWriter

class astropy.utils.xml.writer.XMLWriter(file) [edit on github][source]

A class to write well-formed and nicely indented XML.

Use like this:

w = XMLWriter(fh)
with w.tag('html'):
    with w.tag('body'):
        w.data('This is the content')

Which produces:

<html>
 <body>
  This is the content
 </body>
</html>

Methods Summary

comment(comment) Adds a comment to the output stream.
end([tag, indent, wrap]) Closes the current element (opened by the most recent call to start).
start(tag[, attrib]) Opens a new element.
get_indentation_spaces([offset]) Returns a string of spaces that matches the current indentation level.
element(tag[, text, wrap, attrib]) Adds an entire element.
tag(*args, **kwds) A convenience method for use with the with statement:: with writer.tag(‘foo’): writer.element(‘bar’) # </foo> is implicitly closed here Parameters are the same as to start.
object_attrs(obj, attrs) Converts an object with a bunch of attributes on an object into a dictionary for use by the XMLWriter.
flush()
close(id) Closes open elements, up to (and including) the element identified by the given identifier.
get_indentation() Returns the number of indentation levels the file is currently in.
data(text) Adds character data to the output stream.

Methods Documentation

comment(comment) [edit on github][source]

Adds a comment to the output stream.

Parameters :

comment : str

Comment text, as a Unicode string.

end(tag=None, indent=True, wrap=False) [edit on github][source]

Closes the current element (opened by the most recent call to start).

Parameters :

tag : str

Element name. If given, the tag must match the start tag. If omitted, the current element is closed.

start(tag, attrib={}, **extra) [edit on github][source]

Opens a new element. Attributes can be given as keyword arguments, or as a string/string dictionary. The method returns an opaque identifier that can be passed to the close() method, to close all open elements up to and including this one.

Parameters :

tag : str

The element name

attrib : dict of str -> str

Attribute dictionary. Alternatively, attributes can be given as keyword arguments.

Returns :

id : int

Returns an element identifier.

get_indentation_spaces(offset=0) [edit on github][source]

Returns a string of spaces that matches the current indentation level.

element(tag, text=None, wrap=False, attrib={}, **extra) [edit on github][source]

Adds an entire element. This is the same as calling start, data, and end in sequence. The text argument can be omitted.

tag(*args, **kwds) [edit on github][source]

A convenience method for use with the with statement:

with writer.tag('foo'):
    writer.element('bar')
# </foo> is implicitly closed here

Parameters are the same as to start.

static object_attrs(obj, attrs) [edit on github][source]

Converts an object with a bunch of attributes on an object into a dictionary for use by the XMLWriter.

Parameters :

obj : object

Any Python object

attrs : sequence of str

Attribute names to pull from the object

Returns :

attrs : dict

Maps attribute names to the values retrieved from obj.attr. If any of the attributes is None, it will not appear in the output dictionary.

flush() [edit on github][source]
close(id) [edit on github][source]

Closes open elements, up to (and including) the element identified by the given identifier.

Parameters :

id : int

Element identifier, as returned by the start method.

get_indentation() [edit on github][source]

Returns the number of indentation levels the file is currently in.

data(text) [edit on github][source]

Adds character data to the output stream.

Parameters :

text : str

Character data, as a Unicode string.

Page Contents