quandyfactory / dicttoxml

Simple library to convert a Python dictionary or other native data type into a valid XML string.
GNU General Public License v2.0
382 stars 147 forks source link

Support for custom attributes #27

Open stephenHartzell opened 10 years ago

stephenHartzell commented 10 years ago

Maybe this can already be done, but I haven't seen it. Can insert custom attributes other than "type" and "id"?

The reason I bring this up is that this could very easily support converting a json file into a kml file save for that limitation.

rafaelcanovas commented 10 years ago

I was looking for this also, it seems all dict keys are treated like child nodes, maybe we can get keys starting with '@' (since they're invalid XML node names) to be treated as custom attributes, so we end up with something like:

{
    'checkout': {
        '@currency': 'BRL',
        '@text': 10.5
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <checkout type="dict" currency="BRL">
        10.5
    </checkout>
</root>

We might lose attribute types and will only be possible on dicts. Is this something like this already possible?

alberteloyan commented 9 years ago

This would be awesome to have. I need to be able to add 'name' attributes to generate Android externalized string resource files.

Great approach @mstrcnvs, would love to see it implemented.

ghost commented 8 years ago

+1

neetjn commented 6 years ago

Was this ever implemented or at the very least in a fork?

BowenFu commented 6 years ago

Also need it.

neetjn commented 6 years ago

@BowenFu I ended up using the xmler package. For user, checkout https://github.com/neetjn/polymath-engineering-challenge/blob/master/polymath/core.py

BowenFu commented 6 years ago

@neetjn Thanks. That is very helpful.

elebumm commented 5 years ago

Hi guys,

Crazy to think that this issue has gone back all the way to 2014. I have started developing a solution you can see over on #66. Let me know what you guys think. Feedback is much appreciated.

javadev commented 1 year ago
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <checkout type="dict" currency="BRL">
        10.5
    </checkout>
</root>

may be converted to json

{
  "checkout": {
    "-type": "dict",
    "-currency": "BRL",
    "#text": "\n        10.5\n    "
  }
}

https://xmltojson.github.io/

dizballanze commented 3 months ago

You can do that with xmltodict. It supports it both ways.