【问题标题】:Powershell - Mix 2 XML filesPowershell - 混合 2 个 XML 文件
【发布时间】:2013-06-08 13:05:09
【问题描述】:

我已经查看了现有主题,但没有找到任何内容.. 我有 2 个 XML 文件,除了某些行之外几乎相同。 目的是从第一个 XML 中获取值,将其添加到第二个 XML 中,然后第二个将删除第一个 XML 文件。

这是第一个文件的结构

<configuration >
<protocol>
 <NATIVE>
 </NATIVE>
 <ICAP>
 </ICAP>
 <RPC>
  <ClientList>
    <items>
      <item value="X.X.X.X">
      <item value="A.A.A.A">
    </items>
  </ClientList>
</configuration>

和第二个文件:

<configuration >
<protocol>
 <NATIVE>
 </NATIVE>
 <ICAP>
 </ICAP>
 <RPC>
  <ClientList>
    <items>
      <item value>

    </items>
  </ClientList>
</configuration>

目的是获取带有“Item Value = ...”的行并将其插入到第二个 XML 文件中。对于所有可能存在的行

我尝试将值保存到 TXT 文件中,然后像这样从 TXT 导入:

$xmlpath1 = "c:\temp\configuration.xml"
$xmlpath2 = "c:\configuration1.xml"
$xmlpath3 = "c:\temp\output.txt"
[xml]$xd= Get-Content $xmlpath1
[xml]$xd2= Get-Content $xmlpath2

$rpc=($xd.configuration.protocol.RPC.ClientList.items.item  )
$rpc2=($xd2.configuration.protocol.RPC.ClientList.items.item  )

foreach($value in $rpc2){

$value.value >> "c:\temp\output.txt"

}
$txt = get-content $xmlpath3 
foreach($line in $txt){
$a=$xd.configuration.protocol.RPC.ClientList.items["item"]
$new=$xd.CreateElement("item")
[void]$a.AppendChild($new)
$a.setattribute("value","$line")
 $xd.Save($xmlpath1)

}

但不是得到:

<ClientList>
    <items>
      <item value="X.X.X.X">
        <item value="A.A.A.A"/>
        <item value="B.B.B.B"/>
        <item value="C.C.C.C"/>
      </item>
    </items>
  </ClientList>

我明白了:

<ClientList>
    <items>
      <item value="C.C.C.C">     <== Last entry read by PS...
        <item />
        <item />
        <item />

      </item>
    </items>
</ClientList>

如果测试了很多不同的方法,但都没有成功。 如果您有任何建议,那就太好了!

【问题讨论】:

  • 为我们提供一致且有效的 xml-samples 是一个好的开始。结果中的项目与输入文件不匹配,并且输入文件不完整。

标签: xml powershell import export add


【解决方案1】:

这是一种方法,您的文件包含无效的 xml,修复它们并尝试一下。

$newXml = @"
<configuration>
<protocol>
<NATIVE></NATIVE>
<ICAP></ICAP>
 <RPC>
  <ClientList>
    <items>
        $(
            $xd.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
            $xd2.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
        )
    </items>
  </ClientList>
   </RPC>
   </protocol>
</configuration>
"@

$newXml | Out-File $xmlpath2
Remove-Item $xmlpath1 -Force

【讨论】:

  • 您好,感谢您的回复和发现@"...."@。 Select Node不选择任何东西,out文件是,除了@“...”@的内容,是空的。此外,这 2 个 XML 文件有 114 行,有很多设置。我的第一个想法是将它包含在一个不专门用于此的脚本中。我将深入研究 SelectNode 小程序/命令 Thx
【解决方案2】:

你可以试试这样的。它从test1.xml 中的clientlist 节点获取所有item 元素,并将它们添加到test2.xml

$xml1 = [xml](Get-Content .\Desktop\test1.xml)
$xml2 = [xml](Get-Content .\Desktop\test2.xml)

#foreach item-nodes with value attribute in xml1
$xml1.SelectNodes("//configuration/protocol/RPC/ClientList/items/item[@value]") | % { 
    #check if item already exists. If not, add
    if($xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items/item[@value='$($_.value)']") -eq $null) {
        $xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items").AppendChild($xml2.CreateElement("item")).SetAttribute("value",$_.value)
    }
}

$xml2.Save("C:\Users\graimer\Desktop\test2.xml")

我之前和之后添加了 xml 文件,因为我必须在它们成为有效的 xml 之前修改它们(关闭一些元素)。

test1.xml(之前)

<configuration>
<protocol>
 <NATIVE></NATIVE>
 <ICAP></ICAP>
 <RPC>
  <ClientList>
    <items>
      <item value="X.X.X.X" />
      <item value="A.A.A.A" />
    </items>
  </ClientList>
</RPC>
</protocol>
</configuration>

test2.xml(之前)

<configuration >
<protocol>
 <NATIVE>
 </NATIVE>
 <ICAP>
 </ICAP>
 <RPC>
  <ClientList>
    <items>
    </items>
  </ClientList>
</RPC>
</protocol>
</configuration>

test2.xml(之后)

<configuration>
  <protocol>
    <NATIVE>
    </NATIVE>
    <ICAP>
    </ICAP>
    <RPC>
      <ClientList>
        <items>
          <item value="X.X.X.X" />
          <item value="A.A.A.A" />
        </items>
      </ClientList>
    </RPC>
  </protocol>
</configuration>

【讨论】:

  • 哇。我还有很多工作要准确理解 XML 和 PS 的工作原理……你的脚本运行良好。条件是 XML 文件必须具有相同的大小(行数等...)非常感谢!我会努力的
  • np ;) 如果您认为这是正确答案,请使用复选标记进行确认。我不确定你在说什么条件。上面的脚本不需要相同的文件大小、行数等。 test1 和 test2 可以不同,只要它们遵循相同的方案并且有效(所有元素都关闭)。它只会修改客户端列表,并确保 test1 中的每个项目都在 test2 中创建(不创建重复项)。
  • 我会将问题标记为已解决:) 我说条件是因为我对此有误。但一切都很好。再次感谢
猜你喜欢
  • 1970-01-01
  • 2022-07-03
  • 2015-09-24
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2017-12-19
  • 2022-07-26
  • 1970-01-01
相关资源
最近更新 更多