Coder Social home page Coder Social logo

Comments (8)

quandyfactory avatar quandyfactory commented on August 18, 2024

Would you like for your custom item name to replace "item" in all instances?

from dicttoxml.

murielsilveira avatar murielsilveira commented on August 18, 2024

No. I was thinking in a custom name for each list.
I don't know what is the best way to achieve this, but I was thinking something like this:

dict_obj = {
    'my_list': [
        {
            'foo': 'Abc',
            'bar': 123
        },
        {
            'foo': 'Def',
            'bar': 456
        }
    ],
    'other_list': [
        {
            'foo': 'Abc',
            'bar': 123
        },
        {
            'foo': 'Def',
            'bar': 456
        }
    ],
    'another_list': [
        {
            'foo': 'Abc',
            'bar': 123
        },
        {
            'foo': 'Def',
            'bar': 456
        }
    ]
}

list_item_names_configuration = {
    'my_list': 'my_item_name',
    'other_list': 'my_other_name'
}

xml = dicttoxml.dicttoxml(dict_obj, list_item_names=list_item_names_configuration)

that will produce something like:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <my_list>
        <my_item_name>
            <foo>Abc</foo>
            <bar>123</bar>
        </my_item_name>
        <my_item_name>
            <foo>Def</foo>
            <bar>456</bar>
        </my_item_name>
    </my_list>
    <other_list>
        <my_other_name>
            <foo>Abc</foo>
            <bar>123</bar>
        </my_other_name>
        <my_other_name>
            <foo>Def</foo>
            <bar>456</bar>
        </my_other_name>
    </other_list>
    <another_list>
        <item>
            <foo>Abc</foo>
            <bar>123</bar>
        </item>
        <item>
            <foo>Def</foo>
            <bar>456</bar>
        </item>
    </another_list>
</root>

from dicttoxml.

mbingel avatar mbingel commented on August 18, 2024

My first day with dicttoxml and I also have custom item names on my wishlist.

My solution for dicttoxml would be a mapping for item names of parents.

<order>
  <order_items> 
    <item>
      <id>1</id>
    </item>
  </order_items>
</order>

order_items => order_item

<order>
  <order_items> 
    <order_item>
      <id>1</id>
    </order_item>
  </order_items>
</order>

For a quick fix I replace all item with the parent name stripping the last character (usually the plural s)

dom = parseString(orders)
items = dom.getElementsByTagName('item')
for item in items:
    dom.renameNode(item, '', item.parentNode.nodeName[:-1])
print(dom.toprettyxml())

from dicttoxml.

quandyfactory avatar quandyfactory commented on August 18, 2024

Thank you for your contribution. I'm working on an implementation for this and should have something in the next few weeks or so.

from dicttoxml.

badsequel avatar badsequel commented on August 18, 2024

Any progress on this?

from dicttoxml.

quandyfactory avatar quandyfactory commented on August 18, 2024

I'm not sure when I'll have a chance to implement this functionality. If someone doesn't want to wait and sends a pull request, I'll happily consider it.

from dicttoxml.

quandyfactory avatar quandyfactory commented on August 18, 2024

Not sure if it meets your specific needs, but version 1.7 includes the optional ability to define a function for naming items, taking the list parent element name as an argument.

from dicttoxml.

theshahzaibc avatar theshahzaibc commented on August 18, 2024

I was having same issue and fixed using item_func
I have tried list_item_names but it's not a valid parameter for dicttoxml() function.
So, you can use the same.
Something like this:

list_item_names_config = lambda x: 'custom-item-name'

Xml = dicttoxml(dataobject, custom_root='custom-root', item_func=list_item_names_config,
                                     attr_type=False)

from dicttoxml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.