使用Powershell将CSV转换为特定XML

时间:2012-03-07 19:33:53

标签: xml powershell csv

我正在尝试使用powershell将csv转换为xml表。有没有办法可以准确指定xml将输出的内容。

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>

数据可能没有排列100%,我没有从同一个地方抓取每个样品,但你应该得到漂移。这样的事情可能吗?

另外......我维持一个大型的主-csv。每小时都会添加两条记录。是否可以像这样巧妙地使csv-to-xml增量,或者每次都必须针对master-csv运行脚本。我知道它可以与master-csv一起运行,但随着时间的推移,随着列表的不断增长,它会花费更长的时间。如果我只能添加新条目,那么它会更快。

2 个答案:

答案 0 :(得分:1)

这有用吗?

 $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

答案 1 :(得分:1)

以下脚本以递增方式将数据添加到xml(使用正则表达式和替换功能)。 csv文件input.csv应仅包含新条目。

# filename: test.ps1

function Get-ScriptDirectory
{

$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$csvpath = join-path (get-scriptdirectory) input.csv
$xmlpath = join-path (get-scriptdirectory) output.xml

if (!(test-path $xmlpath)) {
  @"
  <?xml version="1.0" encoding="UTF-8" ?> 
  <chart>
  <series>
  </series>
  <graphs>
  <graph gid="0" color="#32CD32" axis="right" title="Temperature">
  </graph>
  <graph gid="1" color="#FFD700" axis="left" title="Humidity">
  </graph>
  <graph gid="2" color="#8B008B" axis="left" title="Dewpoint">
  </graph>
  </graphs>
  </chart>
"@ | out-file -encoding "UTF8" $xmlpath
}

$xml = [System.IO.File]::ReadAllText($xmlpath)



$data = import-csv $csvpath

$counter = [int](([regex]‘xid="(\d*)"’).matches($xml) | foreach {$_.Groups[1].Value} | %{[int] $_} | sort | select -last 1)
$series = $null;$temp_graph = $null;$humid_graph = $null;$dewpt_graph = $null

$data | %{
$counter++
$series += @"
 <value xid="$counter">$($_.TIME)</value>

"@

 $temp_graph += @"
 <value xid="$counter">$($_.TEMP)</value>

"@

 $humid_graph += @"
 <value xid="$counter">$($_.HUMID)</value>

"@

 $dewpt_graph += @"
 <value xid="$counter">$($_.DEWPT)</value>

"@
}
$xml = $xml -replace "</series>","$series</series>"
$xml = $xml -replace "</graph>\r*\n*\s*<graph gid=`"1`"","$temp_graph</graph><graph gid=`"1`""
$xml = $xml -replace "</graph>\r*\n*\s*<graph gid=`"2`"","$humid_graph</graph><graph gid=`"2`""
$xml = $xml -replace "</graph>\r*\n*\s*</graphs>","$dewpt_graph</graph></graphs>"

$xml | out-file -encoding "UTF8" $xmlpath