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.
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.
|
|
__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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insert(self,
e,
i=None,
attrib=None,
nsmap=None,
**kwargs)
Insert a new child node. |
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
|
|
|
|
|
|
|
|
|
Inherited from object:
__format__,
__getattribute__,
__hash__,
__new__,
__reduce__,
__reduce_ex__,
__repr__,
__sizeof__,
__subclasshook__
|