Package: classyxml
ClassyXmlAn xml file parser that makes the element text and attributes of an xml file accessible as ClassyXml class attributes.Install it from PyPIpipinstallclassyxmlUsageParsing an XML file<!-- countries.xml --><root><countryname="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighborname="Austria"direction="E"/><neighborname="Switzerland"direction="W"/></country><countryname="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighborname="Malaysia"direction="N"/></country></root>fromclassy_xmlimportClassyXml# Load the countries.xml fileclassy=ClassyXml('countries.xml')# Print the XML informationprint(len(classy.country))# Output: 2print(classy.country[0].name)# Output: 'Liechtenstein'print(classy.country[0].rank.text)# Output: '1'print(classy.country[0].year.text)# Output: '2008'print(classy.country[0].gdppc.text)# Output: '141100'print(len(classy.country[0].neighbor))# Output: 2print(classy.country[0].neighbor[0].name)# Output: 'Austria'print(classy.country[0].neighbor[0].direction)# Output: 'E'print(classy.country[0].neighbor[1].name)# Output: 'Switzerland'print(classy.country[0].neighbor[1].direction)# Output: 'W'print(classy.country[1].name)# Output: 'Singapore'print(classy.country[1].rank.text)# Output: '4'print(classy.country[1].year.text)# Output: '2011'print(classy.country[1].gdppc.text)# Output: '59900'print(len(classy.country[1].neighbor))# Output: 1print(classy.country[1].neighbor.name)# Output: 'Malaysia'print(classy.country[1].neighbor.direction)# Output: 'N'Generating an XML fileThis will generate thecountries.xmlfile illustrated above.fromclassy_xmlimportClassyXml,XmlElement# Create an empty ClassyXml objectclassy=ClassyXml()# Creating Liechtenstein Element# Setting the text and attributes using the XmlElement argumentsliechtenstein=XmlElement(attributes={'name':'Liechtenstein'})liechtenstein.rank=XmlElement(text=1)liechtenstein.year=XmlElement(text=2008)liechtenstein.gdppc=XmlElement(text=141100)liechtenstein.neighbor=XmlElement(attributes={'name':'Austria','direction':'E'})liechtenstein.neighbor=XmlElement(attributes={'name':'Switzerland','direction':'W'})# Creating Singapore Element# Setting the text and attributes directlysingapore=XmlElement()singapore.name='Singapore'singapore.rank=XmlElement()singapore.rank.text=4singapore.year=XmlElement()singapore.year.text=2011singapore.gdppc=XmlElement()singapore.gdppc.text=59900singapore.neighbor=XmlElement()singapore.neighbor[0].name='Malaysia'singapore.neighbor[0].direction='N'# Add the countries to ClassyXmlclassy.country=liechtensteinclassy.country=singapore# Save the XML fileclassy.save_as('countries.xml')Modifying an existing XML fileThis will add a USA element to thecountries.xmlfile illustrated above.fromclassy_xmlimportClassyXml,XmlElement# Load the countries.xml fileclassy=ClassyXml('countries.xml')# Create a USA country elementusa=XmlElement(attributes={'name':'USA'})usa.rank=XmlElement(text=2)usa.year=XmlElement(text=2012)usa.gdppc=XmlElement(text=12345)usa.neighbor=XmlElement(attributes={'name':'Canada','direction':'N'})usa.neighbor=XmlElement(attributes={'name':'Mexico','direction':'S'})# Add the USA country element the ClassyXml countriesclassy.country=usa# Save the XML fileclassy.save()