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

First edition of custom attributes. Work in Progress #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,41 @@ Starting in version 1.7.1, you can wrap values in CDATA by setting the optional

If you do not set `cdata` to `True`, the default value is `False` and values are not wrapped.

Adding Custom Attributes
========================

You can add custom attributes to nodes by adding a child `dict` named `@attrs` to a parent `dict` like so:

```python3
my_dict = {
"Family": {
"@attrs": {
"Tree": "Menelaws"
},
"Name": "Lewis Menelaws",
"Occupation": "Programmer",
"Programming Language": "Python"
}
}
```
The key will be the name of the custom attribute while the value will be the value of that custom attribute.

Running a standard `dicttoxml` you will get a result like this:

```xml
<?xml version="1.0" encoding="UTF-8" ?>
<FamilyTree>
<Family Tree="Menelaws">
<Name>Lewis Menelaws</Name>
<Occupation>Programmer</Occupation>
<Programming_Language>Python</Programming_Language>
</Family>
</FamilyTree>
```




Debugging
=========

Expand Down
25 changes: 18 additions & 7 deletions dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
LOG.info('Looping inside convert_dict(): key="%s", val="%s", type(val)="%s"' % (
unicode_me(key), unicode_me(val), type(val).__name__)
)

attr = {} if not ids else {'id': '%s' % (get_unique_id(parent)) }

key, attr = make_valid_xml_name(key, attr)

if isinstance(val, numbers.Number) or type(val) in (str, unicode):
Expand All @@ -225,12 +223,25 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata):
elif isinstance(val, dict):
if attr_type:
attr['type'] = get_xml_type(val)
addline('<%s%s>%s</%s>' % (
key, make_attrstring(attr),
convert_dict(val, ids, key, attr_type, item_func, cdata),
key

if list(val.keys())[0] == '@attrs':
# If first item is @attrs

custom_attrs = val['@attrs']
val.pop('@attrs', None)
addline('<%s%s>%s</%s>' % (
key, make_attrstring(custom_attrs),
convert_dict(val, ids, key, attr_type, item_func, cdata),
key
)
)
else:
addline('<%s%s>%s</%s>' % (
key, make_attrstring(attr),
convert_dict(val, ids, key, attr_type, item_func, cdata),
key
)
)
)

elif isinstance(val, collections.Iterable):
if attr_type:
Expand Down