pdk.XmlMappers ($Date: 2002/12/04 10:13:05 $)
index
pdk/XmlMappers.py

Classes mapping Python dictionaries to DOM nodes.

These classes are used for efficient read/write access to the nodes of an XML data base (see pdk.DataBaseClasses module).

FOG 11/2000

 
Classes
            
pdk.DomClasses.DomTreePointer(pdk.DomClasses.DomTree)
XmlMapper
ChildAttributeMapperById
ChildAttributeMapperByKey
NodeAttributeMapper
 
class ChildAttributeMapperById(XmlMapper)
     
Purpose:maps a Python dictionary to a single attribute of the child nodes of a DOM node using the "id" attribute of the latter
Detail:

this is for XML sections like this:

<root id="rootidstring">
    <child id="idstring1" foo="value1"/>
    <child id="idstring2" foo="value2"/>
</root>

which corresponds to the following mapped Python dictionary:

{ idstring1 : value1,
  idstring2 : value2
  }

The tag name of the children is given in the childNodeName parameter.


 
  
Method resolution order:
ChildAttributeMapperById
XmlMapper
pdk.DomClasses.DomTreePointer
pdk.DomClasses.DomTree

Methods defined here:
__init__(self, node, childNodeName, mappedAttributeNameT, mappedNodeFilterD={})
mapFrom(self)
mapTo(self, id2MappedAttributeValueD)
 
class ChildAttributeMapperByKey(XmlMapper)
     
Purpose:maps a Python dictionary to a single attribute of the child nodes of a DOM node using an arbitrary key attribute of the latter
Detail:

this is for XML sections like this:

<root id="rootidstring">
    <child key="uniquevalue1" foo="value1"/>
    <child key="uniquevalue2" foo="value2"/>
</root>

which corresponds to the following mapped Python dictionary:

{ uniquevalue1 : value1,
  uniquevalue2 : value2
  }

The tag name of the children is given in the childNodeName parameter.


 
  
Method resolution order:
ChildAttributeMapperByKey
XmlMapper
pdk.DomClasses.DomTreePointer
pdk.DomClasses.DomTree

Methods defined here:
__init__(self, node, childNodeName, mappedAttributeName, keyAttributeName)
mapFrom(self)
mapTo(self, keyAttr2MappedAttrD)
 
class NodeAttributeMapper(XmlMapper)
     
Purpose:maps a Python dictionary to (selected) attributes of a DOM node

 
  
Method resolution order:
NodeAttributeMapper
XmlMapper
pdk.DomClasses.DomTreePointer
pdk.DomClasses.DomTree

Methods defined here:
__init__(self, node, *mappedAttributeNameT)
mapFrom(self, contextNode=None)
mapTo(self, attributeD, contextNode=None)
 
class XmlMapper(pdk.DomClasses.DomTreePointer)
     
Purpose:maps the keys and values in a Python dictionary to and from the attributes of a DOM node

 
  
Method resolution order:
XmlMapper
pdk.DomClasses.DomTreePointer
pdk.DomClasses.DomTree

Methods defined here:
mapFromNode(self, node, *mappedAttributeNames)
maps the attributes of the DOM node node into a Python dictionary. mappedAttributeNames should contain attribute names which then specify which attributes to extract.
mapFromNodeListById(self, nodeL, *mappedAttributeNames)
complements the method .mapToNodeListById.
mapFromNodeListByKey(self, nodeL, keyAttributeName, mappedAttributeName)
complements the method .mapToNodeListByKey.
mapToNode(self, node, attributeD, *mappedAttributeNames)
maps the (key,value) pairs in attributeD to attributes of the DOM node node. mappedAttributeNames should contain attribute names which then specify which keys in attributeD to process.
mapToNodeListById(self, id2DataD, *mappedAttributeNames)

map the content of the mapping id2DataD to the nodes specified by the ID strings that are keys of id2DataD. The values of the mapping are treated differently depending on the value of mappedAttributeNames:

  1. contains a single attribute name: mapping values are assigned as attribute of this name to each node. Thus, the mapping

    { "id1" : "value1" , "id2" : "value2" }
    

    with mappedAttributeNames == ("foo",) is mapped to

    <roottag>
        <childtag id="id1" foo="value1"/>
        <childtag id="id2" foo="value2"/>
    </roottag>
    
  2. contains no ore more than one attribute names: mapping values are taken to be mappings themselves and the attributes of each node are updated accordingly (ALL attributes if mappedAttributeNames is empty). Example:

    { "id1" : { "foo" : "foovalue1",
                "bar" : "barvalue1" },
      "id2" : { "foo" : "foovalue2",
                "bar" : "barvalue2" } }
    

    is mapped to

    <roottag>
        <childtag id="id1" foo="foovalue1" bar="barvalue1"/>
        <childtag id="id2" foo="foovalue2" bar="barvalue2"/>
    </roottag>
    

    if mappedAttributeNames was empty or set to ("foo","bar").

mapToNodeListByKey(self, nodeL, keyAttributeName, keyAttr2MappedAttrD, mappedAttributeName)

maps the contents of the mapping keyAttrValue2MappedAttrValueD to attributes of the nodes in nodeL:

  1. get a list of the values of the attribute keyAttributeName from the nodes in nodeL
  2. iterate over the mapping keyAttrValue2MappedAttrValueD; for each key, find the child that has a matching value of the keyAttributeName attribute and update its mappedAttributeName attribute with the value corresponding to this key

This is the general case of the .mapToNodeListById method in that it lets you pick any attribute to serve as key for the update of the mapped attribute, not just the "id" attribute.

 
Author
            
$Author: gathmann $