[Opensrf-commits] r1142 - trunk/src/python/osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Nov 26 08:51:32 EST 2007


Author: erickson
Date: 2007-11-26 08:33:10 -0500 (Mon, 26 Nov 2007)
New Revision: 1142

Modified:
   trunk/src/python/osrf/utils.py
Log:
added ability to parse attributes and elements containing no children or data to the xml2object parser

Modified: trunk/src/python/osrf/utils.py
===================================================================
--- trunk/src/python/osrf/utils.py	2007-11-21 22:01:42 UTC (rev 1141)
+++ trunk/src/python/osrf/utils.py	2007-11-26 13:33:10 UTC (rev 1142)
@@ -26,43 +26,56 @@
 
     for nodeChild in xmlNode.childNodes:
         if nodeChild.nodeType == xmlNode.ELEMENT_NODE:
+            subObj = osrfXMLNodeToObject(nodeChild);
+            __appendChildNode(obj, nodeName, nodeChild.nodeName, subObj)
+            done = True
 
-            # If a node has element children, create a new sub-object 
-            # for this node, attach an array for each type of child
-            # and recursively collect the children data into the array(s)
+    for attr in xmlNode.attributes.values():
+        __appendChildNode(obj, nodeName, attr.name, dict([(attr.name, attr.value)]))
+        
 
-            if not obj.has_key(nodeName):
-                obj[nodeName] = {}
+    if not done and len(xmlNode.childNodes) > 0:
+        # If the node has no element children, clean up the text 
+        # content and use that as the data
+        textNode = xmlNode.childNodes[0] # extract the text node
+        data = unicode(textNode.nodeValue).replace('^\s*','')
+        data = data.replace('\s*$','')
 
-            sub_obj = osrfXMLNodeToObject(nodeChild);
+        if nodeName in obj:
+            # the current element contains attributes and text
+            obj[nodeName]['#text'] = data
+        else:
+            # the current element contains text only
+            obj[nodeName] = data
 
-            if not obj[nodeName].has_key(nodeChild.nodeName):
-                # we've encountered 1 sub-node with nodeChild's name
-                obj[nodeName][nodeChild.nodeName] = sub_obj[nodeChild.nodeName]
+    return obj
 
-            else:
-                if isinstance(obj[nodeName][nodeChild.nodeName], list):
-                    # we already have multiple sub-nodes with nodeChild's name
-                    obj[nodeName][nodeChild.nodeName].append(sub_obj[nodeChild.nodeName])
 
-                else:
-                    # we already have 1 sub-node with nodeChild's name, make 
-                    # it a list and append the current node
-                    val = obj[nodeName][nodeChild.nodeName]
-                    obj[nodeName][nodeChild.nodeName] = [ val, sub_obj[nodeChild.nodeName] ]
+def __appendChildNode(obj, nodeName, childName, subObj):
+    """ If a node has element children, create a new sub-object 
+        for this node, attach an array for each type of child
+        and recursively collect the children data into the array(s) """
 
-            done = True
+    if not obj.has_key(nodeName):
+        obj[nodeName] = {}
 
-    if not done:
-        # If the node has no element children, clean up the text content 
-        # and use that as the data
-        xmlNode = xmlNode.childNodes[0] # extract the text node
-        data = re.compile('^\s*').sub('', unicode(xmlNode.nodeValue))
-        data = re.compile('\s*$').sub('', data)
+    if not obj[nodeName].has_key(childName):
+        # we've encountered 1 sub-node with nodeChild's name
+        if childName in subObj:
+            obj[nodeName][childName] = subObj[childName]
+        else:
+            obj[nodeName][childName] = None
 
-        obj[nodeName] = data
+    else:
+        if isinstance(obj[nodeName][childName], list):
+            # we already have multiple sub-nodes with nodeChild's name
+            obj[nodeName][childName].append(subObj[childName])
 
-    return obj
+        else:
+            # we already have 1 sub-node with nodeChild's name, make 
+            # it a list and append the current node
+            val = obj[nodeName][childName]
+            obj[nodeName][childName] = [ val, subObj[childName] ]
 
 
 def osrfObjectFindPath(obj, path, idx=None):



More information about the opensrf-commits mailing list