Something similar to PHP's SimpleXML in Python?

Is there a way in Python to handle XML files similar to the way PHP's SimpleXML extension does them?

Ideally I just want to be able to access certain xml datas from a list object.

Asked by: Hailey589 | Posted: 28-01-2022

Answer 1

There is a long list of libraries associated with XML processing on the Python wiki. Note that a number of them are included in the standard library. Most of them will do what you are looking for:

to access certain xml datas from a list object

which is a bit vague, and perhaps some more concrete use-case might narrow down that list for you.

Answered by: Sam686 | Posted: 01-03-2022

Answer 2

You might want to try xml.etree.ElementTree It provides many easy ways to access read xml and to build new xml.

Or better yet use lxml.etree that provides even more convenient ways of accessing the nodes (such as xpath)

Answered by: Sam107 | Posted: 01-03-2022

Answer 3

You might be referring to something like this:

I haven't used it myself, but I was also looking for something like simplexml in PHP and this link showed up.

Answered by: Miller658 | Posted: 01-03-2022

Answer 4

lxml.objectify does exactly what you want

In [1]: from lxml import objectify

In [2]: x = objectify.fromstring("""<response><version>1.2</version><amount>1.01</amount><currency>USD</currency></response>""")

In [3]: x.version
Out[3]: 1.2

In [4]: x.amount
Out[4]: 1.01

In [5]: x.currency
Out[5]: 'USD'

Answered by: Justin521 | Posted: 01-03-2022

Similar questions

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python