On 08/02/2012 06:25 PM, Wayne Sun wrote:
When xml node have both attribute and value at first level, the
parser
will broke. After fix, the node key will have a dictionary with both
value and attr inside. For example, the xml node:
<capacity unit='bytes'>536870912000</capacity>
will be parsed into:
{u'capacity': {'attr': {u'unit': u'bytes'},
'value': u'536870912000'}}
Also when fetch the attribute key, should use a new param (attrkey)
other than exist key in outside loop.
Signed-off-by: Wayne Sun <gsun(a)redhat.com>
---
utils/xml_parser.py | 22 +++++++++++++++++-----
1 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/utils/xml_parser.py b/utils/xml_parser.py
index 04e7501..01b928f 100644
--- a/utils/xml_parser.py
+++ b/utils/xml_parser.py
@@ -88,15 +88,21 @@ class xml_parser(object):
if thenode.attributes != None:
tmpattr = dict()
if thenode.attributes.length > 0:
- for key in thenode.attributes.keys():
+ for attrkey in thenode.attributes.keys():
tmpattr.update(
- {key:thenode.attributes.get(key).nodeValue})
+ {attrkey:thenode.attributes.get(attrkey).nodeValue})
attrdic = { "attr":tmpattr }
if key in out:
if out[key] == None:
- out[key] = value
if attrdic != None:
- out[key].update(attrdic)
+ if value == None:
+ out[key] = attrdic
+ else:
+ valdic = { "value":value }
+ valdic.update(attrdic)
+ out[key] = valdic
+ else:
+ out[key] = value
elif type(out[key]) == list:
if attrdic != None:
newdict.update(attrdic)
@@ -111,7 +117,13 @@ class xml_parser(object):
else:
out[key] = value
if attrdic != None:
- out[key].update(attrdic)
+ if value == None:
+ newdict[key] = attrdic
+ else:
+ valdic = { "value":value }
+ valdic.update(attrdic)
+ newdict = valdic
+ out[key] = newdict
self.parseintodict(thenode, level+1, out, key)
return out
That's good, thanks
ACK and pushed.