Which Python XML library should I use?

I am going to process the XML files for the project. I previously decided to use lxml, but after reading the requirements, I think that ElemenTree will be better for my purpose.

The XML files that need to be processed are as follows:

  • Small size. Typically <10K.

  • No namespaces.

  • Simple XML structure.

Given the small size of XML, memory is not a problem. My only concern is a quick analysis.

What should I go with? Mostly I saw people recommend lxml, but given my parsing requirements, can I really benefit from this, or will ElementTree serve my purpose better?

+3
source share
3 answers

, lxml API ElementTree, ElementTree lxml, .

ElementTree, , , Python 2.5 Python, () / C.

+2

lxml - ElementTree, ElementTree, , , lxml.

, ,

0

XML- Python " Python" ActiveState

This does not speed up the parsing. But it provides truly native access to object styles.

>>> SAMPLE_XML = """<?xml version="1.0" encoding="UTF-8"?>
... <address_book>
...   <person gender='m'>
...     <name>fred</name>
...     <phone type='home'>54321</phone>
...     <phone type='cell'>12345</phone>
...     <note>&quot;A<!-- comment --><![CDATA[ <note>]]>&quot;</note>
...   </person>
... </address_book>
... """
>>> address_book = xml2obj(SAMPLE_XML)
>>> person = address_book.person


person.gender        -> 'm'     # an attribute
person['gender']     -> 'm'     # alternative dictionary syntax
person.name          -> 'fred'  # shortcut to a text node
person.phone[0].type -> 'home'  # multiple elements becomes an list
person.phone[0].data -> '54321' # use .data to get the text value
str(person.phone[0]) -> '54321' # alternative syntax for the text value
person[0]            -> person  # if there are only one <person>, it can still
                                # be used as if it is a list of 1 element.
'address' in person  -> False   # test for existence of an attr or child
person.address       -> None    # non-exist element returns None
bool(person.address) -> False   # has any 'address' data (attr, child or text)
person.note          -> '"A <note>"'
0
source

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


All Articles