﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Powershell  |  Web Creators Hub</title>
	<atom:link href="https://web-creators-hub.com/category/windows/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://web-creators-hub.com</link>
	<description>WEB技術などの情報をわかりやすく配信するメディア</description>
	<lastBuildDate>Wed, 08 Feb 2023 01:56:31 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.8.2</generator>
	<item>
		<title>【簡単】PowershellでXML構造ファイルをオブジェクト化して読み込む</title>
		<link>https://web-creators-hub.com/windows/powershell/xml/</link>
		<pubDate>Tue, 25 Feb 2020 07:51:27 +0000</pubDate>
		<dc:creator><![CDATA[taizo]]></dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://web-creators-hub.com/?p=684</guid>
		<description><![CDATA[PowershellではXMLファイルを簡単に扱うことができます。XMLファイルを見やすく別のファイルに保存するときなどに利用します。 &#038;n...]]></description>
				<content:encoded><![CDATA[<p>PowershellではXMLファイルを簡単に扱うことができます。XMLファイルを見やすく別のファイルに保存するときなどに利用します。<br />
&nbsp;</p>
<h2>XMLファイルの内容をオブジェクトにする</h2>
<p>XMLの構造はツリー構造です。フォルダ構造とほぼ同じなのでフォルダのPAHTを記載するようにデータにアクセスします。</p>
<p>例えばPATHをオブジェクト風に記載すると以下のようになります。<br />
・PATH<br />
C:/Users/test/Desktop/test<br />
↓<br />
・オブジェクト風<br />
pathObj.Users.test.Desktop.test</p>
<p>では実際にpowershellを利用しXMLをオブジェクト化します。<br />
以下のサンプルXMLをデスクトップにtest.xmlファイルとして保存してください。</p>
<p>&nbsp;</p>
<h3>サンプルXML</h3>
<pre class="brush: bash; title: ; notranslate">
&lt;TestList version=&quot;2&quot; revision=&quot;1&quot;&gt;
	&lt;Item category=&quot;test&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;1&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;テスト1&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;テスト1の内容&lt;/Description&gt;
		&lt;/Item&gt;
	&lt;Item category=&quot;test&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;2&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;テスト2&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;テスト2の内容&lt;/Description&gt;
	&lt;/Item&gt;
	&lt;Item category=&quot;test&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;3&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;テスト3&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;テスト3の内容&lt;/Description&gt;
	&lt;/Item&gt;
	&lt;Item category=&quot;news&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;4&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;ニュース1&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;ニュース1の内容&lt;/Description&gt;
	&lt;/Item&gt;
	&lt;Item category=&quot;news&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;5&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;ニュース2&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;ニュース2の内容&lt;/Description&gt;
	&lt;/Item&gt;
	&lt;Item category=&quot;news&quot;&gt;
		&lt;ID type=&quot;int&quot;&gt;6&lt;/ID&gt;
		&lt;Name type=&quot;string&quot;&gt;ニュース3&lt;/Name&gt;
		&lt;Description type=&quot;text&quot;&gt;ニュース3の内容&lt;/Description&gt;
	&lt;/Item&gt;
&lt;/TestList&gt;
</pre>
<p>コードを記載するためtest.ps1ファイルを作成します。</p>
<p>test.ps1を右クリック→編集をクリックしファイルを開きます。<br />
以下のコードをコピペします。</p>
<pre class="brush: bash; title: ; notranslate">
# 現在のパス取得
$scriptPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

# xmlファイルのPATH
$source = &quot;$scriptPath\test.xml&quot;

# xmlオブジェクト作成
$xmlObj = (Get-Content -path &quot;$source&quot;)
# データはループさせて取得
foreach ($List in $xmlObj.TestList) {
        foreach ($item in $List.Item) {
            echo $item.Name.innerText
        }
}
</pre>
<p>&nbsp;</p>
<h2>説明</h2>
<p>&nbsp;</p>
<h3>1.オブジェクト化</h3>
<pre class="brush: bash; title: ; notranslate">
$scriptPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$source = &quot;$scriptPath\test.xml&quot;
$xmlObj = (Get-Content -path &quot;$source&quot;)
</pre>
<p>XMLファイルをオブジェクト化するためXMLファイルのPATHを取得します。<br />
ファイルのデータを一行づつ取得するGet-Contentコマンドに使用します。これでは普通テキストファイルのような処理になっていまいますのでXMLを扱えるように全体をXMLにキャストします。</p>
<h3>2.要素の値を取得</h3>
<pre class="brush: bash; title: ; notranslate">
foreach ($List in $xmlObj.TestList) {
    foreach ($item in $List.Item) {
       echo $item.Name.innerText
    }
}
</pre>
<p>要素の値を取得する場合はinnerTextプロパティを使用します。<br />
オブジェクト.innerText<br />
&nbsp;</p>
<h3>3.属性の値を取得</h3>
<pre class="brush: bash; title: ; notranslate">
foreach ($List in $xmlObj.TestList) {
    foreach ($item in $List.Item) {
       echo $item.GetAttribute(&quot;category&quot;)
    }
}
</pre>
<p>属性の値を取得する場合はGetAttributeメソッドを使用します。<br />
オブジェクト.GetAttribute(&#8220;属性名&#8221;)</p>
<p>&nbsp;</p>
<h2>特定の条件により処理する</h2>
<p>if文を利用し特定の条件にマッチした値のみ取得します。<br />
例ではItemのcategory属性の値が「test」になっている要素の値を取得します。</p>
<pre class="brush: bash; title: ; notranslate">
# 現在のパス取得
$scriptPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

# xmlファイルのPATH
$source = &quot;$scriptPath\test.xml&quot;

# xmlオブジェクト作成
$xmlObj = (Get-Content -path &quot;$source&quot;)
# データはループさせて取得
foreach ($List in $xmlObj.TestList) {
        foreach ($item in $List.Item) {
            if($item.GetAttribute(&quot;category&quot;) -eq &quot;test&quot;){
             echo $item.Name.innerText
             }
        }
}
</pre>
]]></content:encoded>
			</item>
	</channel>
</rss>
