sanand0 / xmljson

xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
MIT License
121 stars 33 forks source link

Add namespace support #9

Open Charnelx opened 7 years ago

Charnelx commented 7 years ago

Hi.

I tried to add namespace support for BadgerFish converter. The main problem i have faced - support for standard xml library and lxml. Their namespace implementation slightly differs so there is no simple methods to support them both in one way.

Because of a lack of experience my code is messy, sorry.

My implementation stands for one idea - give on the output exact thing that were received on the input.

Now examples of use:

element = fromstring('<root xmlns="http://dot.com"><joe>string</joe></root>')
dict_obj = xmljson.badgerfish.data(element)

print(dict_obj)
>>> OrderedDict([('root', OrderedDict([('@xmlns', {None: 'http://dot.com'}), ('joe', OrderedDict([('@xmlns', {None: 'http://dot.com'}), ('$', 'string')]))]))])

element = xmljson.badgerfish.etree(dict_obj)
print(tostring(element[0]))
>>> b'<root xmlns="http://dot.com"><joe>string</joe></root>'

print(element[0].nsmap) # lxml only!
>>> {None: 'http://dot.com'}
element = fromstring('<abc:root xmlns:abc="http://dot.com" xmlns:xyz="http://com.doc"><xyz:joe>string</xyz:joe></abc:root>')
dict_obj = xmljson.badgerfish.data(element)

print(dict_obj)
>>> OrderedDict([('root', OrderedDict([('@xmlns', {'xyz': 'http://com.doc', 'abc': 'http://dot.com'}), ('joe', OrderedDict([('@xmlns', {'xyz': 'http://com.doc'}), ('$', 'string')]))]))])

element = xmljson.badgerfish.etree(dict_obj)
print(tostring(element[0]))
>>> b'<root xmlns:xyz="http://com.doc" xmlns:abc="http://dot.com"><xyz:joe>string</xyz:joe></root>'

print(element[0].nsmap) # lxml only!
>>> {'xyz': 'http://com.doc', 'abc': 'http://dot.com'}

If you have some questions - ask, will be glad to answer.

sanand0 commented 7 years ago

@Charnelx -- thanks a lot for this!

I like the approach you're taking for BadgerFish. Do you have any suggestions on how we could do this for other conventions? We don't necessarily have to implement them. But deciding on their specification can help iron-out any inconsistencies in the BadgerFish namespace convention as well.

Also, you may want to re-work the test cases ensure the Travis checks pass.

Charnelx commented 7 years ago

@sanand0 thanks for having time to look at my code!

Do you have any suggestions on how we could do this for other conventions?

Yes i have an idea of how to add support for other convention types but this needs total code refactory. The main problem is still support for both xml and lxml.

Also, you may want to re-work the test cases ensure the Travis checks pass.

Yes, i'll do that. It's little strange because locally tests pass without errors

sanand0 commented 7 years ago

@Charnelx

So it's just the GData convention that needs namespaces then.

Charnelx commented 7 years ago

@sanand0

Good, i'll look what we can do with GData then.

fireundubh commented 6 years ago

Has there been any more work done to support namespaces in GData?

sanand0 commented 6 years ago

@fireundubh -- unfortunately, I haven't received a pull request subsequently. No work is happening on namespaces that I know of