Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created March 7, 2013 12:59
Show Gist options
  • Select an option

  • Save dchaplinsky/5107887 to your computer and use it in GitHub Desktop.

Select an option

Save dchaplinsky/5107887 to your computer and use it in GitHub Desktop.
Improvement for original XmlItemExporter of scrapy to enable saving of dicts to xml (so free-form data can be saved to XML too).
from scrapy.contrib.exporter import XmlItemExporter
class DictXmlItemExporter(XmlItemExporter):
def _export_xml_field(self, name, serialized_value):
self.xg.startElement(name, {})
if hasattr(serialized_value, '__iter__'):
if isinstance(serialized_value, dict):
for key in serialized_value:
self._export_xml_field(key, serialized_value[key])
else:
for value in serialized_value:
self._export_xml_field('value', value)
else:
self.xg.characters(serialized_value)
self.xg.endElement(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment