Skip to content

Instantly share code, notes, and snippets.

@maik-s
Created May 23, 2019 19:29
Show Gist options
  • Save maik-s/fc8f1c05101a84c3945f4b0a8b6ed874 to your computer and use it in GitHub Desktop.
Save maik-s/fc8f1c05101a84c3945f4b0a8b6ed874 to your computer and use it in GitHub Desktop.
Simple gist to demonstrate how to use lxml to find elements within an XML string with xpath expressions.
#!/bin/python3
from lxml import etree
from StringIO import StringIO
parser = etree.XMLParser(remove_blank_text=True)
f = StringIO('<foo><bar>test</bar></foo>')
tree = etree.parse(f, parser)
elem = tree.xpath('/foo/bar')
print (elem[0].text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment