Alexander Herrmann avatar Alexander Herrmann committed 0075997

changed xml parsing to basically use this snippet: http://goo.gl/16Vdc

Comments (0)

Files changed (1)

piston/emitters.py

         xml.endDocument()
 
         return stream.getvalue()
-    
-    @staticmethod 
-    def parse_xml(str):
-        data = {}
-        root = etree.fromstring(str)
-        for element in root:
-            if len(element.getchildren()) == 0:
-                data[element.tag] = element.text
-            else:
-                data[element.tag] = XMLEmitter.parse_xml_recursive(element)
-                
-        return data
-        #return parsexml(str)
+
+    @staticmethod
+    def get_dictionary_from_node(node):
+        """
+        Converts the given XML node into a dictionary.
+        @param node    the xml node
+        """
+        result = {}
+        result[node.tag] = []
+        XMLEmitter.xml_to_dict(node, result[node.tag])
+        #reply[node.tag] = {'value':result[node.tag], 'attribs':node.attrib, 'tail':node.tail}
+        return {node.tag: result[node.tag]}
     
     @staticmethod
-    def parse_xml_recursive(element):
-        if len(element.getchildren()) == 0:
-            return element.text
+    def xml_to_dict(node, parent):
+        """
+        Converts the given XML node into a dictionary respecting the given parent.
+        @param node the xml node
+        @param parent the parent resource  
+        """
+        result = {}
+        if len(node):
+            #n = 0
+            for n in list(node):
+                result[node.tag] = []
+                value = XMLEmitter.xml_to_dict(n, result[node.tag])
+                if len(n):
+#                    value = {'value': result[node.tag], 'attributes': n.attrib, 'tail': n.tail}
+                    value = result[node.tag]
+                    parent.append({n.tag: value})
+                else :
+                    parent.append(result[node.tag][0])
         else:
-            data = {}
-            for children in element.getchildren():
-                data[children.tag] = XMLEmitter.parse_xml_recursive(children)
-            return data
-
+            value = {}
+#            value = {'value': node.text, 'attributes': node.attrib, 'tail': node.tail}
+            value = node.text
+            parent.append({node.tag: value})
+        return
+    
+    @staticmethod
+    def parse_xml(str):
+        try:
+            return XMLEmitter.get_dictionary_from_node(etree.fromstring(str))
+        except etree.XMLSyntaxError:
+            raise ValueError
+    
 Emitter.register('xml', XMLEmitter, 'text/xml; charset=utf-8')
 if etree: # if import was successfull
     Mimer.register(XMLEmitter.parse_xml, ('text/xml',))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.