django-rest-framework-xml icon indicating copy to clipboard operation
django-rest-framework-xml copied to clipboard

XML attributes not handled/parsed

Open CoffeeJack opened this issue 8 years ago • 4 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 avatar Jun 08 '16 18:06 CoffeeJack

@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 avatar Mar 10 '17 11:03 kyleobrien91

@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.

Sam-Costigan avatar Nov 27 '17 21:11 Sam-Costigan

Hi all,

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

You can find it on next pull request: https://github.com/jpadilla/django-rest-framework-xml/pull/39

Best, Dario.

bukosabino avatar Jun 03 '19 12:06 bukosabino

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'
        }
    },
]

bukosabino avatar Jun 03 '19 12:06 bukosabino