XML parser writer that maintains attribute order

I need to parse an XML document and then write each node to separate files that support the exact order of the attributes. Therefore, if I have an input file, for example:

<item a="a" b="b" c="c"/>
<item a="a1" b="b2" c="c3"/>

The output should consist of 2 files with each element. Now, if xml.dom.minidom is used - the order of the attributes changes in the output (I can get - <item b="b" c="c" **a="a"**/>)

I found the pxdom lib, it keeps order, but very, very slow (the minidom partition takes 0.08 seconds, the pxdom parsing takes 2.5 seconds).

Are there any other python libraries that can save attributes?

UPD: libarry must also contain upper and lower case. Thus, "Item" is not equal to "item"

+3
source share
2 answers

You may find this question helpful. The bottom line is a summary of the standard xml tools and libraries that most likely will not be able to do this.

+1
source

You can use BeautifulSoup:

>>> from BeautifulSoup import BeautifulSoup as soup

>>> html = '''<item a="a" b="b" c="c"/>
<item a="a1" b="b2" c="c3"/>'''
>>> s = soup(html)
>>> s.findAll('item')
[<item a="a" b="b" c="c"></item>, <item a="a1" b="b2" c="c3"></item>]
0
source

Source: https://habr.com/ru/post/1771060/


All Articles