Comments (8)
Would you like for your custom item name to replace "item" in all instances?
from dicttoxml.
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.
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.
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.
Any progress on this?
from dicttoxml.
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.
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.
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)
- Root and header not formed HOT 1
- dicttoxml vs dict2xml HOT 1
- Converter doesnt support datetime.date HOT 1
- Facing problems with newer versions of python
- to much logging info HOT 1
- Support for CustomCode-Types?
- Importing ABC directly from collections was deprecated and will be removed in Python 3.10. Use collections.abc HOT 8
- converting python dict to xml taking longer time and sometime hanged the system HOT 6
- 'True' is not a valid value for 'boolean' HOT 2
- Option to omit the xml declaration but keep the root or custom_root HOT 3
- How do I disable encoding type? HOT 2
- Transfer ownership HOT 8
- AttributeError: 'int' object has no attribute 'isdigit' HOT 2
- how to prevent xml escaping HOT 1
- AttributeError: module 'collections' has no attribute 'Iterable' HOT 13
- python 3.9.7: `pip install dicttoxml` is installing only `installed-filed.txt` HOT 5
- collections.Iterable is deprecated HOT 2
- python 3.8: Support dropped in 1.7.5 but not mentioned in release notes or docs HOT 1
- Update readme section on debugging HOT 1
- logging issue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dicttoxml.