PowershellでXMLファイルを新規作成してみる

コンピュータ
PowerShellのあれこれ

PowerShellはSystem.Xml.XmlDocumentを使いXMLファイルを作成や編集することができます。
よく使う機能をまとめて記事にしてみました。

新規作成

以下のようなXMLファイルを作成したい。

<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
    <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml" />
  </rootfiles>
</container>

スクリプト

<#
.SYNOPSIS
 XmlWriterのサンプル
#>

using namespace System.Xml

$xmlFile = ".\container.xml"

$xmlns = "urn:oasis:names:tc:opendocument:xmlns:container"

[xml]$doc = [XmlDocument]::new()

$dec = $doc.CreateXmlDeclaration("1.0", $null, $null)
$doc.AppendChild($dec) | Out-Null

$container = $doc.CreateNode("element", "container", $xmlns)
$container.SetAttribute("version", "1.0")

$rootfiles = $doc.CreateNode("element", "rootfiles", $xmlns)
$container.AppendChild($rootfiles) | Out-Null

$rootfile = $doc.CreateNode("element", "rootfile", $xmlns)
$rootfile.SetAttribute("full-path", "OEBPS/content.opf")
$rootfile.SetAttribute("media-type", "application/oebps-package+xml")
$rootfiles.AppendChild($rootfile) | Out-Null

$doc.AppendChild($container) | Out-Null
$doc.Save($xmlFile) | Out-Null

データの構造を表現するので仕方がないですがコード量が多くなりがちです。
この程度サイズのXMLの場合、スクリプトにヒアドキュメントで直接XMLを埋め込んだ方がシンプルで良さそうです。

既存のXMLファイルを読み込みdomオブジェクトを生成

$xmlDoc = [System.Xml.XmlDocument](Get-Content -Encoding UTF8 -Raw XMLファイルのパス)

(以下、$xmlDocにはWindowエレメントが一つあるものとします。)

XMLファイルの保存

$xmlDoc.Save($inFile) 

属性(Attribute)を追加

$val = "セットする値"
$attri = $xmlDoc.CreateAttribute("属性の名前")
$attri.Value = $val
$xmlDoc.Window.Attributes.Append($attri) | Out-Null

属性(Attribute)を変更

$xmlDoc.Window.setAttribute("名前", "値")

エレメントを作成

$child3 = $xmlDoc.CreateElement("エレメント", "ネームスペース")

タグからエレメントを取得

$element = $xmlDoc.getElementsByTagName("タグ名")

指定のエレメントの直前にエレメントを挿入

$dc = $xmlDoc.Window.insertBefore(挿入するエレメント, ここで指定されたエレメントの直前に挿入される)

子エレメントをインデックス指定して取得

$pos = $xmlDoc.getElementsByTagName("Grid")[インデックス]

コメント