Python XML
   1 min read


Using the lxml Python library to parse XML

Install lxml on Windows

See the latest version of lxml.
In a command prompt window, run: pip install lxml

Use etree to transform an XML string

Use etree.fromstring() to convert an xml string to a lxml element.
Use etree.tostring() to convert a lxml element back to an xml string.

Example:

from lxml import etree    #import the lxml etree module


xmlString = "<record><booktitle>Dark sacred night</booktitle><author>Connelly, Michael</author><publisher>Little, Brown and Company</publisher><publication_date></publication_date></record>"


xmlRootElement = etree.fromstring(xmlString) #get root element
xmlRootElement.find("publication_date").text = "2018" #set root element text


print(etree.tostring(xmlRootElement, encoding='unicode', pretty_print=True))

XML string

Example:

from lxml import etree    #import the lxml etree module


def parseXML(filePath):
    rootXmlObj = etree.parse(filePath)   #parse file
    print(etree.tostring(rootXmlObj, encoding='unicode', pretty_print=True))


parseXML("C:\Users\Desktop\Python files\darksn.xml")

XML string

Example:

from lxml import etree    #import the lxml etree module


bib = etree.Element('bib')
record = etree.SubElement(bib, 'record')
etree.SubElement(record, 'booktitle').text = 'Dark sacred night'
etree.SubElement(record, 'isbn').text = '9780316484800'
etree.SubElement(record, 'author').text = 'Connelly, Michael'
etree.SubElement(record, 'publication_place').text = 'New York, NY'
etree.SubElement(record, 'publisher').text = 'Little, Brown and Company'
etree.SubElement(record, 'publication_date').text = '2018'


declaration = '<?xml version='1.0' encoding='utf-8'?>'
print(declaration)
print(etree.tostring(bib, encoding='unicode', pretty_print=True))

XML string