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

How to form a nested xml if the dict contains several list #56

Open
coodec opened this issue Apr 27, 2017 · 2 comments
Open

How to form a nested xml if the dict contains several list #56

coodec opened this issue Apr 27, 2017 · 2 comments

Comments

@coodec
Copy link

coodec commented Apr 27, 2017

I have this problem, I need to create someting like this:

<root>
    <item1>value</item1>
    <item2>value</item2>
    <item3>value</item3>
    <item4>
         <i4>value</i4>
         <i4>value1</i4>
         <i4>value2</i4>
    </item4>
    <item5>
         <i5>value</i5>
         <i5>value1</i5>
         <i5>value2</i5>
    </item5>
</root>

item4 is a list and i4 is a dict.

Mostly it works fine, if I set:

my_item_func = lambda x: 'i4'
xml = dicttoxml.dicttoxml(form, item_func=my_item_func)

And the key is named 'item4' I have the desired results.

The problem is that the items in the second list i5 are named also i4, as I can set only one function to name the items.

I tried to set the item func as:

my_item_func = lambda x: x
But I get item4 and item5 instead of i4 and i5.

There is any way to get the desired results.

Thanks a lot.

@bilgedemirkaya
Copy link

Did anyone solve this problem?

@javadev
Copy link

javadev commented Jan 20, 2023

<root>
   <item1>value</item1>
   <item2>value</item2>
   <item3>value</item3>
   <item4>
      <i4>value</i4>
      <i4>value1</i4>
      <i4>value2</i4>
   </item4>
   <item5>
      <i5>value</i5>
      <i5>value1</i5>
      <i5>value2</i5>
   </item5>
</root>

may be converted to json

{
  "root": {
    "item1": "value",
    "item2": "value",
    "item3": "value",
    "item4": {
      "i4": [
        "value",
        "value1",
        "value2"
      ]
    },
    "item5": {
      "i5": [
        "value",
        "value1",
        "value2"
      ]
    }
  },
  "#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

No branches or pull requests

3 participants