Open ssato opened 7 years ago
Pattern 2, 6 and 7 looks OK (although key for texts are not '#text' but '@text' by default) from the beginning, and now pattern 1 and 5 becomes OK by the commit a96121d and related ones.
In [3]: xml_s = """\
...: <?xml version="1.0" encoding="UTF-8"?>
...: <config name='foo'>
...: <a>0</a>
...: <b id="b0">bbb</b>
...: <c/>
...: <sect0>
...: <d>x, y, z</d>
...: </sect0>
...: <list1>
...: <item>0</item>
...: <item>1</item>
...: <item>2</item>
...: </list1>
...: </config>
...: """
In [4]: xml_d = anyconfig.loads(xml_s, ac_parser="xml")
In [5]: xml_d
Out[5]:
{'config': {'@attrs': {'name': 'foo'},
'a': '0',
'b': {'@attrs': {'id': 'b0'}, '@text': 'bbb'},
'c': None,
'list1': {'@children': [{'item': '0'}, {'item': '1'}, {'item': '2'}]},
'sect0': {'d': 'x, y, z'}}}
In [6]:
About 3, 4 and 6, I'm not sure that these are worth implementing and do not have side effects.
I was wrong that 6 is not OK, however, I suspect that it might bring unwanted side effects. The commit d42428b brings minor updates:
In [2]: xml_s = """<?xml version="1.0" encoding="UTF-8"?>
...: <config name='foo'>
...: <a>0</a>
...: <b id="b0">bbb</b>
...: <c/>
...: <sect0>
...: <d>x, y, z</d>
...: </sect0>
...: <list1>
...: <item>0</item>
...: <item>1</item>
...: <item>2</item>
...: </list1>
...: <list2 id="list2">
...: <item>i</item>
...: <item>j</item>
...: </list2>
...: </config>
...: """
In [3]: xml_d = anyconfig.loads(xml_s, ac_parser="xml")
In [4]: pprint.pprint(xml_d)
{'config': {'@attrs': {'name': 'foo'},
'a': '0',
'b': {'@attrs': {'id': 'b0'}, '@text': 'bbb'},
'c': None,
'list1': [{'item': '0'}, {'item': '1'}, {'item': '2'}],
'list2': {'@attrs': {'id': 'list2'},
'@children': [{'item': 'i'}, {'item': 'j'}]},
'sect0': {'d': 'x, y, z'}}}
In [5]:
XML backend does not follow 'the spec', http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html such as xmltodict (https://github.com/martinblech/xmltodict) looks so.
from http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html