Created
March 7, 2013 12:59
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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