From 8aa80670869a5df01030670ea6825d1fe19d5d1b Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 26 Nov 2007 13:33:10 +0000 Subject: [PATCH] added ability to parse attributes and elements containing no children or data to the xml2object parser git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1142 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/osrf/utils.py | 67 ++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/python/osrf/utils.py b/src/python/osrf/utils.py index e07ba7a..ed24a87 100644 --- a/src/python/osrf/utils.py +++ b/src/python/osrf/utils.py @@ -26,43 +26,56 @@ def osrfXMLNodeToObject(xmlNode): 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): -- 2.43.2