Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed extra newlines in pretty print mode.

Fixes #54.
  • Loading branch information...
commit 3ba8cdfb39911e22aa110df43983132618a72d30 1 parent e4bd90b
Martín Blech authored
Showing with 28 additions and 14 deletions.
  1. +21 −11 tests/test_dicttoxml.py
  2. +7 −3 xmltodict.py
32 tests/test_dicttoxml.py
View
@@ -6,6 +6,7 @@
import unittest
import re
import collections
+from textwrap import dedent
_HEADER_RE = re.compile(r'^[^\n]*\n')
@@ -92,17 +93,26 @@ def test_attr_order_roundtrip(self):
xml = '<root a="1" b="2" c="3"></root>'
self.assertEqual(xml, _strip(unparse(parse(xml))))
- def test_pretty_print(self):
- obj = {'a': {'b': {'c': 1}}}
- newl = '_newl_'
- indent = '_indent_'
- xml = ('<a>%(newl)s%(indent)s<b>%(newl)s%(indent)s%(indent)s<c>1</c>'
- '%(newl)s%(indent)s</b>%(newl)s</a>')
- xml = xml % {
- 'newl': newl, 'indent': indent
- }
- self.assertEqual(xml, _strip(unparse(obj, pretty=True,
- newl=newl, indent=indent)))
+ if hasattr(collections, 'OrderedDict'):
+ def test_pretty_print(self):
+ obj = {'a': OrderedDict((
+ ('b', [{'c': [1, 2]}, 3]),
+ ('x', 'y'),
+ ))}
+ newl = '\n'
+ indent = '....'
+ xml = dedent('''\
+ <?xml version="1.0" encoding="utf-8"?>
+ <a>
+ ....<b>
+ ........<c>1</c>
+ ........<c>2</c>
+ ....</b>
+ ....<b>3</b>
+ ....<x>y</x>
+ </a>''')
+ self.assertEqual(xml, unparse(obj, pretty=True,
+ newl=newl, indent=indent))
def test_encoding(self):
try:
10 xmltodict.py
View
@@ -281,18 +281,22 @@ def _emit(key, value, content_handler,
attrs[ik[len(attr_prefix):]] = iv
continue
children.append((ik, iv))
- if pretty and depth:
- content_handler.ignorableWhitespace(newl + indent * depth)
+ if pretty:
+ content_handler.ignorableWhitespace(depth * indent)
content_handler.startElement(key, AttributesImpl(attrs))
+ if pretty and children:
+ content_handler.ignorableWhitespace(newl)
for child_key, child_value in children:
_emit(child_key, child_value, content_handler,
attr_prefix, cdata_key, depth+1, preprocessor,
pretty, newl, indent)
if cdata is not None:
content_handler.characters(cdata)
+ if pretty and children:
+ content_handler.ignorableWhitespace(depth * indent)
content_handler.endElement(key)
if pretty and depth:
- content_handler.ignorableWhitespace(newl + indent * (depth - 1))
+ content_handler.ignorableWhitespace(newl)
def unparse(input_dict, output=None, encoding='utf-8', **kwargs):
Please sign in to comment.
Something went wrong with that request. Please try again.