Package: xml-stream
xml_streamA simple XML file and string reader that is able to read big XML files and strings by using streams (iterators),
with an option to convert to dictionariesDescriptionxml_streamcomprises two helper functions:read_xml_fileWhen given a path to a file and the name of the tag that holds the relevant data, it returns an iterator
of the data asxml.etree.ElementTree.Elementobject by default, or as dicts whento_dictargument isTrueread_xml_stringWhen given an XML string and the name of the tag that holds the relevant data, it returns an iterator
of the data asxml.etree.ElementTree.Elementobject by default, or as dicts whento_dictargument isTrueMain DependenciesPython +3.6Getting StartedInstall the packagepipinstallxml_streamImport theread_xml_fileand theread_xml_stringclasses and use accordinglyfromxml_streamimportread_xml_file,read_xml_stringxml_string="""<company><staff><operations_department><employees><team>Marketing</team><location name="head office" address="Kampala, Uganda" /><bio first_name="John" last_name="Doe">John Doe</bio><bio first_name="Jane" last_name="Doe">Jane Doe</bio><bio first_name="Peter" last_name="Doe">Peter Doe</bio></employees><employees><team>Customer Service</team><location name="Kampala branch" address="Kampala, Uganda" /><bio first_name="Mary" last_name="Doe">Mary Doe</bio><bio first_name="Harry" last_name="Doe">Harry Doe</bio><bio first_name="Paul" last_name="Doe">Paul Doe</bio></employees></operations_department></staff></company>"""file_path='...'# path to your XML file# For XML strings, use read_xml_string which returns an iteratorforelementinread_xml_string(xml_string,records_tag='staff'):# returns the element as xml.etree.ElementTree.Element by default# ...do something with the elementprint(element)# Note that if a tag is namespaced with say _prefix:tag_ and domain is _xmlns:prefix="https://example",# the records_tag from that tag will be '{https://example}tag'forelement_as_dictinread_xml_string(xml_string,records_tag='staff',to_dict=True):# returns the element as dictionary# ...do something with the element dictionaryprint(element_as_dict)# will print"""{'operations_department': {'employees': [[{'team': 'Marketing','location': {'name': 'head office','address': 'Kampala, Uganda'},'first_name': 'John','last_name': 'Doe','_value': 'John Doe'},{'team': 'Marketing','location': {'name': 'head office','address': 'Kampala, Uganda'},'first_name': 'Jane','last_name': 'Doe','_value': 'Jane Doe'},{'team': 'Marketing','location': {'name': 'head office','address': 'Kampala, Uganda'},'first_name': 'Peter','last_name': 'Doe','_value': 'Peter Doe'}, ],[{'team': 'Customer Service','location': {'name': 'Kampala branch','address': 'Kampala, Uganda'},'first_name': 'Mary','last_name': 'Doe','_value': 'Mary Doe'},{'team': 'Customer Service','location': {'name': 'Kampala branch','address': 'Kampala, Uganda'},'first_name': 'Harry','last_name': 'Doe','_value': 'Harry Doe'},{'team': 'Customer Service','location': {'name': 'Kampala branch','address': 'Kampala, Uganda'},'first_name': 'Paul','last_name': 'Doe','_value': 'Paul Doe'}],]}}"""# For XML files (even really large ones), use read_xml_file which also returns an iteratorforelementinread_xml_file(file_path,records_tag='staff'):# returns the element as xml.etree.ElementTree.Element by default# ...do something with the elementprint(element)forelement_as_dictinread_xml_file(file_path,records_tag='staff',to_dict=True):# returns the element as dictionary# ...do something with the element dictionaryprint(element_as_dict)# see the print output for read_xml_stringHow to testClone the repo and enter its root foldergitclonehttps://github.com/sopherapps/xml_stream.git&&cdxml_streamCreate a virtual environment and activate itvirtualenv-p/usr/bin/python3.6env&&sourceenv/bin/activateInstall the dependenciespipinstall-rrequirements.txtDownload a huge xml file for test purposes and save it in the/testfolder ashuge_mock.xmlwgethttp://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/data/SwissProt/SwissProt.xml&&mvSwissProt.xmltest/huge_mock.xmlRun the test commandpython-munittestAcknowledgementsThisStack Overflow Answerabout converting XML to dict was very helpful.ThisReal Python tutorial on publishing packageswas very helpfulLicenseCopyright (c) 2020Martin AhinduraLicensed under theMIT License