diff --git a/plexpy/exporter.py b/plexpy/exporter.py index bcb2861f..6c707141 100644 --- a/plexpy/exporter.py +++ b/plexpy/exporter.py @@ -1598,7 +1598,7 @@ class Export(object): outfile.write(json_data) elif self.file_format == 'xml': - xml_data = helpers.dict2xml(result, root_node=self.media_type) + xml_data = helpers.dict2xml({self.media_type: result}, root_node='export') with open(filepath, 'w', encoding='utf-8') as outfile: outfile.write(xml_data) diff --git a/plexpy/helpers.py b/plexpy/helpers.py index 67b9f1d1..eda2f820 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -1385,7 +1385,7 @@ def escape_xml(value): # https://gist.github.com/reimund/5435343/ -def dict2xml(d, root_node=None): +def dict2xml(d, root_node=None, indent=4, level=0): wrap = not bool(root_node is None or isinstance(d, list)) root = root_node or 'objects' root_singular = root[:-1] if root.endswith('s') and isinstance(d, list) else root @@ -1395,28 +1395,34 @@ def dict2xml(d, root_node=None): if isinstance(d, dict): for key, value in sorted(d.items()): if isinstance(value, dict): - children.append(dict2xml(value, key)) + children.append(dict2xml(value, key, level=level + 1)) elif isinstance(value, list): - children.append(dict2xml(value, key)) + children.append(dict2xml(value, key, level=level + 1)) else: xml = '{} {}="{}"'.format(xml, key, escape_xml(value)) elif isinstance(d, list): for value in d: - children.append(dict2xml(value, root_singular)) + # Custom tag replacement for collections/playlists + if isinstance(value, dict) and root in ('children', 'items'): + root_singular = value.get('type', root_singular) + children.append(dict2xml(value, root_singular, level=level)) else: children.append(escape_xml(d)) end_tag = '>' if len(children) > 0 else '/>' + end_tag += '\n' if isinstance(d, list) or isinstance(d, dict) else '' + spaces = ' ' * level * indent if wrap or isinstance(d, dict): - xml = '<{}{}{}'.format(root, xml, end_tag) + xml = '{}<{}{}{}'.format(spaces, root, xml, end_tag) if len(children) > 0: for child in children: xml = '{}{}'.format(xml, child) if wrap or isinstance(d, dict): - xml = '{}'.format(xml, root) + spaces = spaces if isinstance(d, dict) else '' + xml = '{}{}\n'.format(xml, spaces, root) return xml