# - - - w r i t e X M L F i l e
def writeXMLFile ( txny ):
'''Write the XML representation of the taxonomy.
[ txny is a Txny instance ->
if file xml-file(Args()) can be opened new for writing ->
file xml-file(Args)) := an XML representation of txny
else ->
Log() +:= error message(s)
stop execution ]
'''
The reader should be familiar with the XML generation techniques
described in Python XML
processing with lxml. In particular, the
factory instance E builds new XML elements. We
will also use et, which is a replacement for
Python's standard elementTree module.
We'll start by building the et.Document instance,
whose root is a taxonomySystem element.
#-- 1 --
# [ root := a new rnc.TAXONOMY_SYSTEM_N et.Element
# doc := a new et.ElementTree with that root element ]
root = et.Element(rnc.TAXONOMY_SYSTEM_N, version=EXTERNAL_VERSION)
doc = et.ElementTree ( root )
There are four required child elements. The first, representing
the rank hierarchy, is built by Section 15.9, “Hier.writeXML(): Generate XML”.
#-- 2 --
# [ root := root with an rnc.RANK_SET_N element added
# representing txny.hier ]
txny.hier.writeXML ( root )
The second child of the root, containing the taxonomic tree, is
build by Section 18.9, “TaxaTree.writeXML(): Generate XML
output”.
#-- 3 --
# [ root := root with an rnc.TAXONOMY_N element added
# representing the tree rooted in txny.taxaTree.root ]
txny.taxaTree.writeXML ( root )
The third and fourth children, representing valid and collision
codes, are generated by Section 24.5, “AbTab.writeXML()”.
That completes the tree, so we can write it out.
#-- 4 --
# [ root := root with an rnc.ABBR_SET_N element added
# representing codes in txny.abTab linked to specific
# taxa, and an rnc.COLLISION_SET_N element added
# representing collision codes in txny.abTab ]
txny.abTab.writeXML ( root )
#-- 5 --
# [ if file (Args().xmlFileName) can be opened new for writing ->
# that file := doc as XML
# else ->
# Log() +:= error message
# stop execution ]
try:
xmlFile = file ( Args().xmlFileName, 'w' )
doc.write ( xmlFile, pretty_print=True )
xmlFile.close()
except IOError, detail:
Log().fatal ( "Can't open the output XML file '%s': %s" %
(Args.xmlFilName, detail) )