Module lxmlmate :: Class ObjectifiedElementProxy
[hide private]
[frames] | no frames]

Class ObjectifiedElementProxy

source code

object --+
         |
        ObjectifiedElementProxy

Proxy class for objectify.ObjectifiedElement instance which can be created by objectify.Element() or SubElement() or XML() or fromstring().

main purpose is to intercept AttributeException when access a non-existent tag.

How to access xml tag

.
such as root.element.name
[]
such as root['element']['name']
hybrid
such as root['element'].name
note
The tag can only be accessed by [] when it is one of the reserved keywords. Tag not in the xml tree can be accessed directly. A new tag will be created. No exceptions will be raised.

How to access xml tag's attributes

[]
.attrib['style'], an exception will be raised when style dos'nt exist. .attrib['style'] = 'big'
.get/set
.attrib.get( 'style', None ) .attrib.set( 'style', 'big' )

How to access class attributes and methods

.
attributes are reserved keywords and they can only be accessed by this way, for example .pyval .text .insert etc. or they are considered xml tags rather than attributes.

Reserved keywords

The following keywords are used as methods or attributes' names.

pyval : returns the python value carried by leaf tag. read-only.

text : returns leaf tag's text content. read-only.

obj : returns ObjectifiedElement object referenced by this class instance. read-only.

tag : returns tag names. can be modified by . way such as *.tag='newTagName'. readable and writable.

attrib : returns tag attributes dict. readable and writeable.

parent : returns parent node. read-only.

children : returns all children's list. read-only.

len : returns the number of children.

insert : insert a child node at the specified position.

remove : remove a child node at the specified position.

index : returns the position of the specified object.

swap : swap two nodes' position.

Examples

create a brand new xml:

>>> p = ObjectifiedElmentProxy( rootag='Person' )
>>> p.name = 'peter'
>>> p.age = 13
>>> print( p )
<Person>
    <name>peter</name>
    <age>13</age>
</Person>

create from xml string:

>>> p = ObjectifiedElementProxy( xmlStr="<Person><name>peter</name><age>13</age></Person>" )
>>> print( p )
<Person>
    <name>peter</name>
    <age>13</age>
</Person>

multiple levels examples:

>>> r = ObjectifiedElementProxy()
>>> r.person.name = 'jack'
>>> r.person.age = 10
>>> print( r )
<root>
    <person>
        <name>jack</name>
        <age>10</age>
    </person>
</root>

to insert child like '<person><name>peter</name><age>13</age></person>':

>>> r.insert( 'person' )('name','peter')('age',13)
>>> p = r('person').person[-1]
>>> p.name = 'david'
>>> p.age = 16
>>> print( r )
<root>
    <person>
        <name>jack</name>
        <age>10</age>
    </person>
    <person>
        <name>peter</name>
        <age>13</age>
    </person>
    <person>
        <name>david</name>
        <age>16</age>
    </person>
</root>
>>> print( r.station[1].name.pyval )
peter

Notes

xml attrib names and tag names are case insensitive.

Nodes with text attribute are called leaf nodes. Theoretically, leaf nodes should not have children, but not required.

Instance Methods [hide private]
 
__init__(self, objectifiedElement=None, xmlFileName=None, xmlStr=None, rootag='root', attrib=None, nsmap=None, **kwargs)
initialize from ObjectifiedElement or xml file or xml string or create a brand new.
source code
 
__call__(self, tag, pyval=None, attrib=None, nsmap=None, **kwargs)
Insert a new child node.
source code
 
__getattr__(self, name) source code
 
__setattr__(self, name, value)
x.__setattr__('name', value) <==> x.name = value
source code
 
__delattr__(self, e)
x.__delattr__('name') <==> del x.name
source code
 
__len__(self)
children's number
source code
 
__getitem__(self, name) source code
 
__setitem__(self, e, v) source code
 
__delitem__(self, e) source code
 
insert(self, e, i=None, attrib=None, nsmap=None, **kwargs)
Insert a new child node.
source code
 
swap(self, i, j)
swap two child nodes' position.
source code
 
remove(self, i)
remove the child node.
source code
 
index(self, o)
return the position of o.
source code
 
xpath(self, path)
find elements list in accordance with path.
source code
 
__nonzero__(self) source code
 
is_empty(self)
To determine whether a null node.
source code
 
clean(self)
clean all null nodes.
source code
 
tostring(self, encoding='utf-8', xml_declaration=True, standalone=None, with_comments=True, pytype=False, xsi=True, xsi_nil=True, cleanup_namespaces=True, doctype=None, with_tail=True, exclusive=False, inclusive_ns_prefixes=None) source code
 
__str__(self)
str(x)
source code
 
dump(self, xmlFile, encoding='utf-8')
save xml to file.
source code

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

Properties [hide private]
  children
  parent
  root
  obj
  pyval

Inherited from object: __class__

Method Details [hide private]

__init__(self, objectifiedElement=None, xmlFileName=None, xmlStr=None, rootag='root', attrib=None, nsmap=None, **kwargs)
(Constructor)

source code 

initialize from ObjectifiedElement or xml file or xml string or create a brand new.

Arguments

objectifiedElement : ObjectifiedElement, optional
an ObjectifiedElement object.
xmlFileName : str, optional
xml's filename.
xmlStr : str, optional
xml's content.
rootag : str, optional
create ObjectifiedElement instance which root tag's name is rootag.
attrib, nsmap, kwargs : optional
refer to objectify.Element()
Overrides: object.__init__

__call__(self, tag, pyval=None, attrib=None, nsmap=None, **kwargs)
(Call operator)

source code 

Insert a new child node.

insert a new child node to the end.

Arguments

e : str
the new tag to be inserted.
pyval : legal python data type
tag's python value.
attrib,nsmap,kwargs : optional
attribs for the new tag. see also objectify.Element() or SubElement().

Returns

ObjectifiedElementProxy instance

See Also

insert

note the difference between the two methods' return values.

Examples

>>> p=ObjectifiedElementProxy( rootag='Person' )
>>> p( 'name', pyval='jack' )('age', pyval=13 )
>>> print( p )
<Person>
        <name py:pytype="str">jack</name>
        <age py:pytype="int">13</age>
</Person>

__setattr__(self, name, value)

source code 
x.__setattr__('name', value) <==> x.name = value
Overrides: object.__setattr__
(inherited documentation)

__delattr__(self, e)

source code 
x.__delattr__('name') <==> del x.name
Overrides: object.__delattr__
(inherited documentation)

insert(self, e, i=None, attrib=None, nsmap=None, **kwargs)

source code 

Insert a new child node.

insert a new child node at the specified position.

Arguments

e : str
the new tag to be inserted.
i : int, optional
if i is integer : position of the new tag. else append to the end.
attrib,nsmap,kwargs : optional
attribs for the new tag. see also objectify.Element() or SubElement().

swap(self, i, j)

source code 

swap two child nodes' position.

Arguments

i,j : int
position of the child nodes to be swapped.

remove(self, i)

source code 

remove the child node.

Arguments

i : int or ObjectifiedElement or ObjectifiedElementProxy or list
position of the child node or Element which will be removed.

index(self, o)

source code 

return the position of o.

Arguments

o : ObjectifiedElementProxy
the ObjectifiedElementProxy instance to be positioned.

Returns

int

xpath(self, path)

source code 
find elements list in accordance with path.

Arguments
---------
path : str
    please refer to lxml.objectify.ObjectifiedElement.xpath.

Returns
-------
list
    a list of ObjectifiedElementProxy instance.


Xpath grammer review
--------------------

==========  ===========
expression  description
==========  ===========   
nodename    to select all children of the node name
/           select from root node.    
//          select from current node
.           select the current code.   
..          select the parent node of the current node.
@           select attrib.
[]          condition
text()      tag text
*           arbitrary node
==========  ============

is_empty(self)

source code 

To determine whether a null node.

no text no attribs no children.

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

dump(self, xmlFile, encoding='utf-8')

source code 

save xml to file.

Arguments

xmlFile : str
xml's filename.

Property Details [hide private]

children

Get Method:
unreachable.children(self, **kwargs)

parent

Get Method:
unreachable.parent(self)

root

Get Method:
unreachable.root(self)

obj

Get Method:
unreachable.obj(self)

pyval

Get Method:
unreachable.pyval(self)