【问题标题】:How to append xml from hash table values in powershell如何在powershell中从哈希表值附加xml
【发布时间】:2012-01-07 05:39:40
【问题描述】:

我有一个哈希表,其中组件名称为键,基线为值。

以下几行描述了我的哈希表

Name                           Value                                                                                                                           
----                           -----                                                                                                                                       
Comp_Bin                       Comp_12_23_2011.1276                                                                            
Complicen                      Comp_11_2_2011.461                                               
SupportComp                    Comp_2.1.0.17.1135                                                                                                  

我想将此值添加到已经可用的 xml 文件中

<Component>
    <Name></Name>
    <Baseline></Baseline>
    <KLOC>0</KLOC>
    <IsCount>True</IsCount>
</Component>

将哈希值添加到xml并使其如下

<Components>
   <Name>Comp_Bin</Name>
   <Baseline>Comp_12_23_2011.1276</Baseline>
   <KLOC>0</KLOC>
   <IsCount>True</IsCount>
</Component>

如何将哈希表转换为xml?请帮帮我。

我已经像下面这样解析了我的哈希表,但是下面的代码失败了,因为每次都必须复制 XML 内容,然后单独更改名称和基线元素

$CCountDoc= [XML] (Get-Content "ccount.xml")
    foreach($key in $($hash.keys)){

     $Baseline = $hash[$key]
     $Name= $key 

     $CCountDoc.Name=$Name
     $CCountDoc.Baseline=$Baseline
    }

【问题讨论】:

    标签: xml powershell powershell-2.0


    【解决方案1】:

    这将处理现有文件。这是您的输入文件:

    <Components>
      <Component>
        <Name></Name>
        <Baseline></Baseline>
        <KLOC>0</KLOC>
        <IsCount>True</IsCount>
      </Component>
    </Components>
    

    会变成:

    <Components>
      <Component>
        <Name>Complicen</Name>
        <Baseline>Comp_11_2_2011.461</Baseline>
        <KLOC>0</KLOC>
        <IsCount>True</IsCount>
      </Component>
      <Component>
        <Name>SupportComp</Name>
        <Baseline>Comp_2.1.0.17.1135</Baseline>
        <KLOC>0</KLOC>
        <IsCount>True</IsCount>
      </Component>
      <Component>
        <Name>Comp_Bin</Name>
        <Baseline>Comp_12_23_2011.1276</Baseline>
        <KLOC>0</KLOC>
        <IsCount>True</IsCount>
      </Component>
    </Components>
    

    这是代码:

    clear 
    $hash = @{ "Comp_Bin" = "Comp_12_23_2011.1276"; "Complicen" = "Comp_11_2_2011.461"; "SupportComp" = "Comp_2.1.0.17.1135" }
    $keys = New-Object object[] $hash.Count
    $hash.Keys.CopyTo($keys,0) 
    
    $xml = [xml] [System.IO.File]::ReadAllText("c:\pst\1.xml")
    $node = $xml.Components.Component.Clone()
    
    $xml.Components.Component.Name = $keys[0]
    $xml.Components.Component.Baseline = $hash[$keys[0]]
    
    for($i = 1; $i -lt $hash.Count; $i++) 
    { 
    $node.Name = $keys[$i]
    $node.Name
    
    $node.Baseline = $hash[$keys[$i]]
    $importNode = $xml.ImportNode($node, $true)  
    $xml.Components.AppendChild($importNode) | Out-Null 
    } 
    $xml.Save("c:\PST\result.xml") 
    

    【讨论】:

      【解决方案2】:

      这似乎正在做你需要的:

      clear 
      $hash = @{ "Comp_Bin" = "Comp_12_23_2011.1276"; "Complicen" = "Comp_11_2_2011.461"; "SupportComp" = "Comp_2.1.0.17.1135" }
      
      $xml = [xml] "<root namespace=`"namespace`"></root>" 
      foreach($key in $hash.Keys) 
      { 
      $insert = [xml] [string]::Format("<Component> 
          <Name>{0}</Name> 
          <Baseline>{1}</Baseline> 
          <KLOC>0</KLOC> 
          <IsCount>True</IsCount> 
          </Component> 
          ", $key, $hash[$key]) 
      $importNode = $xml.ImportNode($insert.DocumentElement, $true)  
      $xml.root.AppendChild($importNode) |Out-Null 
      } 
      $xml.Save("c:\PST\result.xml") 
      

      这将是输出文件:

      <root namespace="namespace">
        <Component>
          <Name>Complicen</Name>
          <Baseline>Comp_11_2_2011.461</Baseline>
          <KLOC>0</KLOC>
          <IsCount>True</IsCount>
        </Component>
        <Component>
          <Name>SupportComp</Name>
          <Baseline>Comp_2.1.0.17.1135</Baseline>
          <KLOC>0</KLOC>
          <IsCount>True</IsCount>
        </Component>
        <Component>
          <Name>Comp_Bin</Name>
          <Baseline>Comp_12_23_2011.1276</Baseline>
          <KLOC>0</KLOC>
          <IsCount>True</IsCount>
        </Component>
      </root>
      

      【讨论】:

      • 这个答案是在 xml 文件中创建所有元素。但在我的情况下,我需要将 Name 和 Baseline 元素添加到我拥有的现有 XML 中。
      【解决方案3】:

      这是使用XmlDocument的一种方式:

      Clear-Host
      $hTable = @{"Comp_Bin"="Comp_12_23_2011.1276";"Complicen"="Comp_11_2_2011.461";"SupportComp"="Comp_2.1.0.17.1135"}
      
      # Create XML root
      [xml]$xmlDoc = New-Object system.Xml.XmlDocument
      $xmlDoc.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><Root></Root>")
      
      
      foreach($entry in $hTable.keys)
      {
        Write-Host 
        # Create a text nod
        $xmlCpn = $xmlDoc.CreateElement("Component")
      
        $xmlElt = $xmlDoc.CreateElement("Name")
        $xmlText = $xmlDoc.CreateTextNode($entry)
        $null = $xmlElt.AppendChild($xmlText)
        $null = $xmlCpn.AppendChild($xmlElt)
      
        $xmlElt = $xmlDoc.CreateElement("Baseline")
        $xmlText = $xmlDoc.CreateTextNode($hTable[$entry])
        $null = $xmlElt.AppendChild($xmlText)
        $null = $xmlCpn.AppendChild($xmlElt)
      
        $xmlElt = $xmlDoc.CreateElement("KLOC")
        $xmlText = $xmlDoc.CreateTextNode("0")
        $null = $xmlElt.AppendChild($xmlText)
        $null = $xmlCpn.AppendChild($xmlElt)
      
        $xmlElt = $xmlDoc.CreateElement("IsCount")
        $xmlText = $xmlDoc.CreateTextNode("True")
        $null = $xmlElt.AppendChild($xmlText)
        $null = $xmlCpn.AppendChild($xmlElt)
      
        # Add the nod to the document
        $null = $xmlDoc.LastChild.AppendChild($xmlCpn);
      }
      
      # Backup to file
      $xmlDoc.Save("c:\Temp\Component.xml")
      

      【讨论】:

      • “将字符串转换为”[xml] 与创建并加载 XmlDocument 完全相同。我说“铸造”是因为[xml] 不是类型,而是类型accelerator
      • 我完全同意你的看法,我只是没有使用它的想法。
      猜你喜欢
      • 2013-02-05
      • 1970-01-01
      • 2021-10-20
      • 2017-02-06
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多