Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple elements of the same name? #59

Open
BobStein opened this issue Aug 29, 2017 · 8 comments · May be fixed by #86
Open

Multiple elements of the same name? #59

BobStein opened this issue Aug 29, 2017 · 8 comments · May be fixed by #86

Comments

@BobStein
Copy link

BobStein commented Aug 29, 2017

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?

@edison12a
Copy link

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

@axfelix
Copy link

axfelix commented Jul 23, 2018

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 :)

@BobStein
Copy link
Author

BobStein commented Jul 27, 2018

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.

@Felixoid
Copy link

Looks like #64 fix it pretty well

@axfelix
Copy link

axfelix commented Oct 21, 2018

Yes, that looks great!

@BobStein
Copy link
Author

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

@bilgedemirkaya bilgedemirkaya linked a pull request Jul 5, 2021 that will close this issue
@edgar-slalom
Copy link

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

@javadev
Copy link

javadev commented Jan 17, 2023

{
   "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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants