[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [test-API][PATCH] Fix xml parser problem when node have both attribute and value



  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 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
 
-- 
1.7.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]