jpadilla / django-rest-framework-xml

XML support for Django REST Framework

Home Page:http://jpadilla.github.io/django-rest-framework-xml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

XML attributes not handled/parsed

CoffeeJack opened this issue · comments

If I have something like:
<?xml version="1.0" encoding="utf-8"?> <foo status="wolololol"> Bar </foo>

The status="wololol" is not captured. Any suggestions as to how I can capture it?

@CoffeeJack - I think this is a very valid concern.

Let's see - perfect scenario, how would you like that represented in a converted data structure?

So, text like the following:

<?xml version="1.0" encoding="utf-8"?>
    <root>
        <field_a>121.0</field_a>
        <field_b>dasd</field_b>
        <field_c></field_c>
        <field_d>2011-12-25 12:45:00</field_d>
    </root>

is parsed into

{
   'field_a': 121,
   'field_b': 'dasd',
   'field_c': None,
   'field_d': datetime.datetime(2011, 12, 25, 12, 45, 00)
}

Assuming the addition of an attribute on one of the root children, how would you envision that represented in the parsed object?

@kyleobrien91 I came across this issue and had to subclass XMLParser and override _xml_convert to solve it. I ended up with something like the following:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <field name="attribute_a">121.0</field_a>
    <field id="0001" name="attribute_b">dasd</field_b>
</root>

Is parsed into:

[
   {'field': 121, 'name': 'attribute_a'},
   {'field': 'dasd', 'id': 0001, 'name': 'attribute_b'},
]

Unfortunately it doesn't work if there was an attribute to parse on the root element, as that attribute would be tacked onto the end of the list. But for my purposes that was fine.

Hi all,

I have fixed this issue doing some changes on XMLParser implementation.

You can find it on next pull request: #39

Best,
Dario.

The idea was to parse something like the following:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <field name="attribute_a">121.0</field_a>
    <field id="0001" name="attribute_b">dasd</field_b>
</root>

Is parsed into:

[
    {'field': {
        'value': 121,
        'attributes': {
            'name': 'attribute_a'
        }
    },
   {'field':
        'value': 'dasd',
        'attributes': {
            'id': '001',
            'name': 'attribute_b'
        }
    },
]