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

XML attributes not handled/parsed #16

Open
CoffeeJack opened this issue Jun 8, 2016 · 4 comments
Open

XML attributes not handled/parsed #16

CoffeeJack opened this issue Jun 8, 2016 · 4 comments

Comments

@CoffeeJack
Copy link

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?

@kyleobrien91
Copy link
Collaborator

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

@Sam-Costigan
Copy link

Sam-Costigan commented Nov 27, 2017

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

bukosabino added a commit to bukosabino/django-rest-framework-xml that referenced this issue Jun 3, 2019
Based on this issue: jpadilla#16

Changes to handle/parse XML attributes.
@bukosabino
Copy link

bukosabino commented Jun 3, 2019

Hi all,

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

You can find it on next pull request: #39

Best,
Dario.

@bukosabino
Copy link

bukosabino commented Jun 3, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants