Package fcp :: Module xmlobject :: Class XMLNode
[hide private]
[frames] | no frames]

Class XMLNode

source code

This is the workhorse for the xml object interface

(If you're viewing the epydoc-generated HTML documentation, click the 'show private' link at the top right of this page to see all the methods)

Instance Methods [hide private]
 
__init__(self, parent, node)
You shouldn't need to instantiate this directly
source code
 
_render(self)
Produces well-formed XML of this node's contents, indented as required
source code
 
__repr__(self) source code
 
__getattr__(self, attr)
Fetches an attribute or child node of this tag
source code
 
__setattr__(self, attr, val)
Change the value of an attribute of this tag
source code
 
_keys(self)
Return a list of attribute names
source code
 
_values(self)
Returns a list of (attrname, attrval) tuples for this tag
source code
 
_items(self)
returns a list of attribute values for this tag
source code
 
_has_key(self, k)
returns True if this tag has an attribute of the given name
source code
 
_get(self, k, default=None)
returns the value of attribute k, or default if no such attribute
source code
 
__len__(self)
returns number of child nodes
source code
 
__getitem__(self, idx)
if given key is numeric, return the nth child, otherwise try to return the child tag (or list of child tags) having the key as the tag name
source code
 
_addNode(self, child)
Tries to append a child node to the tree, and returns it
source code
 
_getChild(self, name)
Returns a list of zero or more child nodes whose tag name is <name>
source code
 
_delChild(self, child)
Removes given child node
source code
 
_addText(self, value)
Tries to append a child text node, with the given text, to the tree, and returns the created node object
source code
 
_addComment(self, comment)
Tries to append a child comment node (with the given text value) to the tree, and returns the create node object
source code
 
_save(self, where=None)
Generates well-formed XML from just this node, and saves it to a file.
source code
 
_toxml(self)
renders just this node out to raw xml code
source code
Method Details [hide private]

__getattr__(self, attr)
(Qualification operator)

source code 

Fetches an attribute or child node of this tag

If it's an attribute, then returns the attribute value as a string.

If a child node, then:

  • if there is only one child node of that name, return it
  • if there is more than one child node of that name, return a list of child nodes of that tag name

Supports some magic attributes:

  • _text - the value of the first child node of type text

__setattr__(self, attr, val)

source code 

Change the value of an attribute of this tag

The magic attribute '_text' can be used to set the first child text node's value

For example:

   Consider:
   
     <somenode>
       <child>foo</child>
     </somenode>

   >>> somenode
   <XMLNODE: somenode>
   >>> somenode.child
   <XMLNODE: child>
   >>> somenode.child._text
   'foo'
   >>> somenode._toxml()
   u'<somenode><child>foo</child></somenode>'
   >>> somenode.child._text = 'bar'
   >>> somenode.child._text
   'bar'
   >>> somenode.child._toxml()
   u'<somenode><child>bar/child></somenode>'

_addNode(self, child)

source code 

Tries to append a child node to the tree, and returns it

Value of 'child' must be one of:

  • a string (in which case it is taken to be the name of the new node's tag)
  • a dom object, in which case it will be wrapped and added
  • an XMLNode object, in which case it will be added without wrapping

_save(self, where=None)

source code 

Generates well-formed XML from just this node, and saves it to a file.

Argument 'where' is either an open file object, or a pathname

If 'where' is not given, then saves the entire document tree.