当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Powershell 合并 XML

作者:精品下载站 日期:2024-12-14 07:16:15 浏览:16 分类:玩电脑

使用 Powershell 合并 XML


假设我们必须每个 XML 文档都包含一个书籍列表(我使用本页中的 XML 示例文件,因为我懒得编写自己的文件),并且我们希望将它们加入到一个 XML 中。

File1.xml 如下所示:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
      <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

File2.XML 如下所示:

<?xml version="1.0"?>
<catalog>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
</catalog>

首先,使用 Get-Content 导入这两个文件并将结果转换为 XML,如下所示:

$File1 = [xml]Get-Content .File1.xml

$File2 = [xml]Get-Content .File2.xml

现在要将一个元素从一个 XML 导入到另一个 XML,我们首先需要使用 ImportNode 对其进行转换,然后使用 AppendChild 方法添加它,否则 powershell 将显示错误,指出“要插入的节点”来自不同的文档上下文。

因此,让我们首先对 $File2 中的每本书执行 foreach 操作,列出我们要导入的每个节点:

Foreach ($Node in $File2.DocumentElement.ChildNodes) {
    $Node
}

然后从 $File1 添加 ImportNode 方法,foreach 将如下所示:

Foreach ($Node in $File2.DocumentElement.ChildNodes) {
    $File1.ImportNode($Node, $true)
}

ImportNode方法有两个参数,详细解释请参见MSDN。

要实际将导入的节点添加到 XML 对象中,我们需要调用 AppendChild 方法。添加后,foreach 循环将如下所示:

Foreach ($Node in $File2.DocumentElement.ChildNodes) {
    $File1.DocumentElement.AppendChild($File1.ImportNode($Node, $true))
}

现在 $File1 包含 File1.xml 和 File2.xml 中的所有节点

完整的脚本如下所示(我已将 XML 添加为此处字符串而不是文件):

[xml]$File1 = @"
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
      <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>
"@

[xml]$File2 = @"
<?xml version="1.0"?>
<catalog>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
</catalog>
"@
ForEach ($XmlNode in $File2.DocumentElement.ChildNodes) {
    $File1.DocumentElement.AppendChild($File1.ImportNode($XmlNode, $true))
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯