quandyfactory/dicttoxml

Multiple elements of the same name?

BobStein opened this issue · 8 comments

I need to generate some XML from a Python dictionary that has multiple elements of the same name.

<country>
    <city>Chicago</city>
    <city>Boston</city>
</country>

Obviously the following won't work. I assumed it would produce a syntax error, but it simply hides one of the keys:

>>> from dicttoxml import dicttoxml
>>> dicttoxml({'country':{'city':'Chicago', 'city': 'Boston'}}, attr_type=False, root=False)
'<country><city>Boston</city></country>'

I tried a list, but that wraps each list element in an item element:

>>> dicttoxml({ 'country': [ {'city': 'Chicago'}, {'city': 'Boston'} ] }, attr_type=False, root=False)
'<country><item><city>Chicago</city></item><item><city>Boston</city></item></country>'

Here's what I want the output to be

'<country><city>Chicago</city><city>Boston</city></country>'

What dictionary and/or function call settings (e.g. item_func=None) would produce that?

Have you tried enabling unique id attributes?: https://github.com/quandyfactory/dicttoxml#unique-id-attributes ?

I think what @BobStein wants is an option to, when producing XML structures from dicts that contain list values, to generate multiple XML sibling elements for each value, rather than a single element with multiple children following the key-item naming pattern that this library currently produces.

I would also like this :)

Yes @axfelix, there is currently no way dicttoxml can generate XML with multiple same-tag-name siblings. (Other than item-tags that is.)

No @SimiCode, that would just elaborate the <item> wrappers with id attributes. I need to generate XML without those wrappers.

Looks like #64 fix it pretty well

Yes, that looks great!

I agree, it looks like this issue would be fixed by #64.

This is exactly what I need fix. Can we merge this one too? Please!!!

{
   "country": [
      {
         "city": "Chicago"
      },
      {
         "city": "Boston"
      }
   ]
}

may be converted to xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <country>
    <city>Chicago</city>
  </country>
  <country>
    <city>Boston</city>
  </country>
</root>
<country>
   <city>Chicago</city>
   <city>Boston</city>
</country>

may be converted to json

{
  "country": {
    "city": [
      "Chicago",
      "Boston"
    ]
  },
  "#omit-xml-declaration": "yes"
}

https://xmltojson.github.io/