Repository: martinblech/xmltodict

Issue:
Parameter to Disable Multiple Root Check
I'm trying to convert a dict to an xml snippet, but this xml snippet is just supposed to be part of a later full document, so it may or may not have one root element. Unfortunately a ValueError is thrown if there is more than one possible root element - it would be great if there was a keyword parameter to disable that check. (Or perhaps when `full_document=False`)

So for example:
```python
xmltodict.unparse({'node': [1,2,3]}, full_document=False)

# would produce something like this without throwing an error:
"""<node>1</node>
<node>2</node>
<node>3</node>
"""
```

Discussion:

Patch:
diff --git a/xmltodict.py b/xmltodict.py
index 4fdbb16..b0ba601 100755
--- a/xmltodict.py
+++ b/xmltodict.py
@@ -318,7 +318,8 @@ def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
     can be customized with the `newl` and `indent` parameters.
 
     """
-    ((key, value),) = input_dict.items()
+    if full_document and len(input_dict) != 1:
+        raise ValueError('Document must have exactly one root.')
     must_return = False
     if output is None:
         output = StringIO()
@@ -326,7 +327,8 @@ def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
     content_handler = XMLGenerator(output, encoding)
     if full_document:
         content_handler.startDocument()
-    _emit(key, value, content_handler, **kwargs)
+    for key, value in input_dict.items():
+        _emit(key, value, content_handler, **kwargs)
     if full_document:
         content_handler.endDocument()
     if must_return:


Test patch:
diff --git a/tests/test_dicttoxml.py b/tests/test_dicttoxml.py
index e449316..4b1d4b8 100644
--- a/tests/test_dicttoxml.py
+++ b/tests/test_dicttoxml.py
@@ -49,10 +49,21 @@ class DictToXMLTestCase(unittest.TestCase):
         self.assertEqual(obj, parse(unparse(obj)))
         self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
 
+    def test_no_root(self):
+        self.assertRaises(ValueError, unparse, {})
+
     def test_multiple_roots(self):
         self.assertRaises(ValueError, unparse, {'a': '1', 'b': '2'})
         self.assertRaises(ValueError, unparse, {'a': ['1', '2', '3']})
 
+    def test_no_root_nofulldoc(self):
+        self.assertEqual(unparse({}, full_document=False), '')
+
+    def test_multiple_roots_nofulldoc(self):
+        obj = OrderedDict((('a', 1), ('b', 2)))
+        xml = unparse(obj, full_document=False)
+        self.assertEqual(xml, '<a>1</a><b>2</b>')
+
     def test_nested(self):
         obj = {'a': {'b': '1', 'c': '2'}}
         self.assertEqual(obj, parse(unparse(obj)))