What's the best way to parse XML in Python?

8 Replies, 1383 Views

Man, I wasted so much time with `minidom` before switching to `lxml`. Trust me, it’s worth the extra install.

```python
from lxml import etree
root = etree.XML('<root><a>Text</a></root>')
print(root.xpath('//a/text()')[0]) # "Text"
```
XPath is a godsend for complex queries.

If you’re on Windows, installing `lxml` can be annoying—use `pip install lxml` and pray, lol.

Messages In This Thread



Users browsing this thread: 1 Guest(s)