Using python to create a data.xml file for fusion charts
I am a newbie to python. I just want to know how i can开发者_Python百科 create a code to write an xml file name data.xml for fusioncharts. The xml file follows the format:
<?xml version="1.0" encoding="UTF-8" ?>
<graph caption="Test graph" xAxisName="X" yAxisName="Y" showAnchors="1" anchorRadius="1" showValues="0">
<set name='2004' value='37800' color='AFD8F8' />
<set name='2005' value='21900' color='F6BD0F' />
<set name='2006' value='32900' color='8BBA00' />
<set name='2007' value='39800' color='FF8E46' />
</graph>
Thanks.
You can use the miniDOM library which is built into python 2.0 and later:
http://docs.python.org/library/xml.dom.minidom.html
ElementTree from stdlib might be helpful:
import xml.etree.cElementTree as etree
G = etree.Element('graph', dict(caption='Test graph', xAxisName="..."))
for name, value, color in get_sets():
etree.SubElement(G, 'set', dict(name=name, value=value, color=color))
etree.ElementTree(G).write(open('data.xml', 'wb'),
encoding='utf-8', xml_declaration=True))
There's a nice chapter on XML in Dive Into Python 3, most of which also applies to Python 2.x if that's what you're working with. The book/website is an excellent overall resource for a lot of other topics when getting started with Python, too.
精彩评论