Commits

Alexander Herrmann committed 8dea42d

XMLEmitter now is able to handle XML input data

Comments (0)

Files changed (1)

piston/emitters.py

     import yaml
 except ImportError:
     yaml = None
+    
+try:
+    from lxml import etree
+except ImportError:
+    etree = None
 
 # Fallback since `any` isn't in Python <2.5
 try:
         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 parse_xml_recursive(element):
+        if len(element.getchildren()) == 0:
+            return element.text
+        else:
+            data = {}
+            for children in element.getchildren():
+                data[children.tag] = XMLEmitter.parse_xml_recursive(children)
+            return data
 
 Emitter.register('xml', XMLEmitter, 'text/xml; charset=utf-8')
-Mimer.register(lambda *a: None, ('text/xml',))
+if etree: # if import was successfull
+    Mimer.register(XMLEmitter.parse_xml, ('text/xml',))
 
 class JSONEmitter(Emitter):
     """