Reputation: 673
I am parsing my xml file using lxml parser which looks like this:
# some elements above
<contact>
<phonenumber>
#something
</phonenumber>
</contact>
I want to be able to return only a part of the xml file.
Like Suppose if I am on phonenumber, I want lxml to return the everything between as a string .
I dont want to return textb/w phonenumber but the entire string :
<phonenumebr>something</phonenumber>
Is it possible ?
Upvotes: 1
Views: 2032
Reputation: 65791
To print a part of the XML tree, you can use lxml.etree.tostring
. On Python 2:
In [1]: from lxml.etree import tostring, parse
In [2]: tree = parse('test.xml')
In [3]: elem = tree.xpath('//phonenumber')[0]
In [4]: print tostring(elem)
<phonenumber>
something
</phonenumber>
For more information you can refer to the "Serialisation" section of the lxml
tutorial.
Upvotes: 1