Package: dict2xml
Super Simple utility to convert a python dictionary into an xml stringInstallationInstall using pip:> python -m pip install dict2xmlexamplefromdict2xmlimportdict2xmldata={'a':1,'b':[2,3],'c':{'d':[{'p':9},{'o':10}],'e':7}}printdict2xml(data,wrap="all",indent="  ")Output<all><a>1</a><b>2</b><b>3</b><c><d><p>9</p></d><d><o>10</o></d><e>7</e></c></all>methodsdict2xml.dict2xml(data, *args, **kwargs)Equivalent to:dict2xml.Converter(*args,**kwargs).build(data)dict2xml.Converter(wrap="",indent="  ", newlines=True)Knows how to convert a dictionary into an xml stringwrap: Wraps the entire tree in this tagindent: Amount to prefix each line for each level of nestingnewlines: Whether or not to use newlinesdict2xml.Converter.build(data, iterables_repeat_wrap=True, closed_tags_for=None, data_sorter=None)Instance method on Converter that takes in the data and creates the xml stringiterables_repeat_wrap - when false the key the array is in will be repeatedclosed_tags_for - an array of values that will produce self closing tagsdata_sorter - an object as explained below for sorting keys in mapsdict2xml.DataSorterAn object used to determine the sorting of keys for a map of data.By default anOrderedDictobject will not have it’s keys sorted, but any
other type of mapping will.It can be made so evenOrderedDictwill get sorted by passing indata_sorter=DataSorter.always().Or it can be made so that keys are produced from the sorting determined by
the mapping withdata_sorter=DataSorter.never().NoteWhen this library was first created python did not have deterministic
sorting for normal dictionaries which is why default everything gets sorted butOrderedDictdo not.To create custom sorting logic requires an object that has a singlekeys_frommethod on it that accepts a map of data and returns a list of strings, where only
the keys that appear in the list will go into the output and those keys must exist
in the original mapping.Self closing tagsTo produce self closing tags (like<item/>) then thebuildmethod must
be given a list of values underclosed_tags_for. For example, if you wantNoneto produce a closing tag then:example={"item1":None,"item2":{"string1":"","string2":None},"item3":"special",}result=Converter("").build(example,closed_tags_for=[None])assertresult==dedent("""
    <item1/>
    <item2>
        <string1></string1>
        <string2/>
    </item2>
    <item3>special</item3>
""").strip())Here onlystring2gets a self closing tag because it has data ofNone,
which has been designated as special.If you want to dynamically work out which tags should be self closing then you
may provide an object that implements__eq__and do your logic there.LimitationsNo attributes on elementsCurrently no explicit way to hook into how to cope with your custom dataCurrently no way to insert an xml declaration lineChangelog1.7.5 - 13 February 2024Introduced thedata_sorteroption1.7.4 - 16 January 2024Make the tests compatible with pytest81.7.3 - 25 Feb 2023This version has no changes to the installed code.This release converts to hatch for packaging and adds a wheel to the
package on pypi.CI will now run against python 3.11 as well1.7.2 - 18 Oct 2022This version has no changes to the installed code.This release adds the tests to the source distribution put onto pypi.1.7.1 - 16 Feb 2022Adding an option to have self closing tags when the value for that
tag equals certain values1.7.0 - 16 April, 2020Use collections.abc to avoid deprecation warning. Thanks @mangin.This library no longer supports Python2 and is only supported for
Python3.6+. Note that the library should still work in Python3.5 as I
have not used f-strings, but the framework I use for the tests is only 3.6+.1.6.1 - August 27, 2019Include readme and LICENSE in the package1.6 - April 27, 2018No code changeschanged the licence to MITAdded more metadata to pypiEnabled travis ciUpdated the tests slightly1.5No changelog was kept before this point.DevelopmentTo enter a virtualenv with dict2xml and dev requirements installed run:> source run.sh activateTests may be run with:> ./test.shOr:> ./run.sh toxLinting and formatting is via:> ./format
> ./lintPython Black will work on the tests as long asNOSE_OF_YETI_BLACK_COMPAT=trueand the correct version of black is available. This is true if your editor
is opened in the same terminal session after sourcing run.sh or if
you make sure that environment variable is set and the editor is using the
virtualenv made by running or sourcingrun.sh(tools/venv/.python)