I want to parse an XML document into the desired format to create data-points for a graph



Here is a sample of what the XML file looks like:



<observation realtime_start="2014-08-01" realtime_end="2014-08-01" date="1947-01-01" value="1939.5"/>
<observation realtime_start="2014-08-01" realtime_end="2014-08-01" date="1948-01-01" value="2020.0"/>
<observation realtime_start="2014-08-01" realtime_end="2014-08-01" date="1949-01-01" value="2009.0"/>
<observation realtime_start="2014-08-01" realtime_end="2014-08-01" date="1950-01-01" value="2184.0"/>
<observation realtime_start="2014-08-01" realtime_end="2014-08-01" date="1951-01-01" value="2360.0"/>


I would like to get it into this format:



{ x: new Date(1949, 01, 1), y: 2009.0},
{ x: new Date(1950, 01, 1), y: 2184.0 },
{ x: new Date(1951, 01, 1), y: 2360.0 },


I am using the minidom library for python to parse the xml file using this code:



for item in dataList:
thefile.write("{ x: new Date(%s)," % item.attributes['date'].value)
thefile.write(" y: %s}, \n" % item.attributes['value'].value)


This outputs:



{ x: new Date(1967-01-01), y: 334047.409},
{ x: new Date(1967-04-01), y: 335653.701},
{ x: new Date(1967-07-01), y: 338440.658},
{ x: new Date(1967-10-01), y: 347027.379},
{ x: new Date(1968-01-01), y: 351784.192},


The problem I have is that the dates are parsed in the format YYYY-MM-DD, and I can't figure out how to change the format in the script.


I am using the canvas.js library to graph if there is a way to change the date formats.


No comments:

Post a Comment