CSV to Specific XML with Powershell -
i trying use powershell convert csv xml table. there way can specify xml output.
example csv input: time,temp,humid,dewpt "03/07/2012 12:04:59",72.1,73.2,62.5 "03/07/2012 11:34:53",71.9,73.8,62.5 "03/07/2012 11:04:46",71.8,74.6,62.7 "03/07/2012 10:34:39",72.3,72.6,62.4 "03/07/2012 10:04:33",71.9,72.1,61.9 example xml output: <?xml version="1.0" encoding="utf-8" ?> - <chart> - <series> <value xid="0">03/07/2012 00:02:06</value> <value xid="1">03/07/2012 00:32:17</value> <value xid="2">03/07/2012 01:02:26</value> <value xid="3">03/07/2012 01:32:35</value> <value xid="4">03/07/2012 02:02:42</value> </series> - <graphs> - <graph gid="0" color="#32cd32" axis="right" title="temperature"> <value xid="0">43.1</value> <value xid="1">44.0</value> <value xid="2">43.6</value> <value xid="3">43.1</value> <value xid="4">42.7</value> </graph> - <graph gid="1" color="#ffd700" axis="left" title="humidity"> <value xid="0">64.6</value> <value xid="1">100.0</value> <value xid="2">57.7</value> <value xid="3">71.6</value> <value xid="4">44.3</value> </graph> <graph gid="2" color="#8b008b" axis="left" title="dewpoint"> <value xid="0">30.4</value> <value xid="1">44</value> <value xid="2">28.4</value> <value xid="3">32.9</value> <value xid="4">22.6</value> </graph> </graphs> </chart>
the data may not line 100%, did not grab each sample piece same place, should drift. possible?
also... maintain large master-csv. every hour 2 records added it. possible make csv-to-xml increment smartly that, or have run script against master-csv each time. know work running against master-csv, worried on time take longer , longer list continues grow. if able add new entries, make go faster.
does work?
$head = @' <?xml version="1.0" encoding="utf-8" ?> <chart> <series> '@ $series = @' '@ $temp_graph = @' </series> <graphs> <graph gid="0" color="#32cd32" axis="right" title="temperature"> '@ $humid_graph = @' <graph gid="1" color="#ffd700" axis="left" title="humidity"> '@ $dewpt_graph = @' <graph gid="2" color="#8b008b" axis="left" title="dewpoint"> '@ $end = @' </graphs> </chart> '@ $i = 0 import-csv file.csv | foreach-object { $series += @" <value xid="$i">$($_.time)</value> "@ $temp_graph += @" <value xid="$i">$($_.temp)</value> "@ $humid_graph += @" <value xid="$i">$($_.humid)</value> "@ $dewpt_graph += @" <value xid="$i">$($_.dewpt)</value> "@ $i++ } $temp_graph,$humid_graph,$dewpt_graph | foreach {$_ += '<\graph>' $head + $series + $temp_graph + $humid_graph + $dewpt_graph + $end
Comments
Post a Comment