Package bitten :: Package util :: Module xmlio :: Class ParsedElement

Class ParsedElement

object --+
         |
        ParsedElement

Representation of an XML element that was parsed from a string or file.

This class should not be used directly. Rather, XML text parsed using xmlio.parse() will return an instance of this class.

>>> xml = parse('<root/>')
>>> print xml.name
root

Parsed elements can be serialized to a string using the write() method:

>>> import sys
>>> parse('<root></root>').write(sys.stdout)
<root/>

For convenience, this is also done when coercing the object to a string using the builtin str() function, which is used when printing an object:

>>> print parse('<root></root>')
<root/>

(Note that serializing the element will produce a normalized representation that may not excatly match the input string.)

Attributes are accessed via the attr member:

>>> print parse('<root foo="bar"/>').attr['foo']
bar

Attributes can also be updated, added or removed:

>>> xml = parse('<root foo="bar"/>')
>>> xml.attr['foo'] = 'baz'
>>> print xml
<root foo="baz"/>
>>> del xml.attr['foo']
>>> print xml
<root/>
>>> xml.attr['foo'] = 'bar'
>>> print xml
<root foo="bar"/>

CDATA sections are included in the text content of the element returned by gettext():

>>> xml = parse('<root>foo<![CDATA[ <bar> ]]>baz</root>')
>>> xml.gettext()
'foo <bar> baz'

Valid input are utf-8 or unicode strings, or any type easily converted to unicode such as integers. Output is always utf-8.

Instance Methods
 
__init__(self, node)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 
children(self, name=None)
Iterate over the child elements of this element.
 
__iter__(self)
 
gettext(self)
Return the text content of this element.
 
write(self, out, newlines=False)
Serializes the element and writes the XML to the given output stream.
 
__str__(self)
Return a string representation of the XML element.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Properties
  name
Local name of the element
  namespace
Namespace URI of the element
  attr

Inherited from object: __class__

Method Details

__init__(self, node)
(Constructor)

 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

children(self, name=None)

 

Iterate over the child elements of this element.

If the parameter name is provided, only include elements with a matching local name. Otherwise, include all elements.

gettext(self)

 

Return the text content of this element.

This concatenates the values of all text and CDATA nodes that are immediate children of this element.

__str__(self)
(Informal representation operator)

 
Return a string representation of the XML element.
Overrides: object.__str__

Property Details

name

Local name of the element
Get Method:
unreachable(self)

namespace

Namespace URI of the element
Get Method:
unreachable(self)