【问题标题】:compare xml structure using Powershell使用 Powershell 比较 xml 结构
【发布时间】:2012-11-05 17:25:09
【问题描述】:

我有两个 xml 配置文件,我只需要比较两个文件的结构并显示差异。 请注意:比较时,应忽略 xml 节点内的值。

例如:

XML 1 
----
<recipe>
  <ingredients>
      <ingredient1></ingredient1><ingredient2></ingredient2>
  </ingredients>
  <description></description>
</recipe>

XML 2
-----
<recipe>
  <ingredients>
    <ingredient1></ingredient1>
  </ingredients>
  <description></description>
  <images></images>
</recipe>

结果应该是两个 xml 文件的差异。

xml1 <ingredient2>
xml2 <images>

非常感谢您的帮助。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我能想到的最快解决方案是:

    [xml]$xml1 = @"
    <recipe>
      <ingredients>
          <ingredient1>
          </ingredient1>
          <ingredient2>
          </ingredient2>
      </ingredients>
      <description></description>
    </recipe>
    "@
    
    [xml]$xml2 = @"
    <recipe>
      <ingredients>
        <ingredient1>dada</ingredient1>
      </ingredients>
      <description>dadad</description>
      <images></images>
    </recipe>
    "@
    
    $docDiffs=Compare-Object ($xml1.SelectNodes("//*") | Select-Object -Expand Name) ($xml2.SelectNodes("//*") | Select-Object -Expand Name) 
    
    $docDiffs
    

    您需要做一些工作才能获得所需的确切格式,但主要工作已经完成。如果您想改进它,您可以考虑使用 Select-XML 替代。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多