Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string dump of lxml Element

Tags:

python

xml

lxml

I have an lxml Element object:

>>> from lxml import etree
>>> xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<state type=\"before_battle\">\n</state>"
>>> etree.fromstring(xml_str.encode('utf-8'))
<Element state at 0x7fd04b957e48>

How to get the string dump of Element?

like image 432
bux Avatar asked Jun 14 '18 20:06

bux


People also ask

Is XML and lxml are same?

lxml is a Python library which allows for easy handling of XML and HTML files, and can also be used for web scraping. There are a lot of off-the-shelf XML parsers out there, but for better results, developers sometimes prefer to write their own XML and HTML parsers.

Is lxml included in Python?

lxml has been downloaded from the Python Package Index millions of times and is also available directly in many package distributions, e.g. for Linux or macOS.

Is lxml secure?

Is lxml safe to use? The python package lxml was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.


Video Answer


1 Answers

first store the element object in a variable

>>> d = etree.fromstring(xml_str.encode('utf-8'))

Then use the tostring function from the lxml.etree module:

>>> etree.tostring(d)
'<state type="before_battle">\n</state>'

For additional use cases, you can check out the lxml.etree Tutorial.

like image 93
nosklo Avatar answered Sep 22 '22 18:09

nosklo