documents:proglang:powershell:powershell-008
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン | |||
| documents:proglang:powershell:powershell-008 [2026/05/08 12:00] – ↷ documents:os:windows:powershell:powershell-008 から documents:proglang:powershell:powershell-008 へページを移動しました。 k896951 | documents:proglang:powershell:powershell-008 [2026/05/13 07:19] (現在) – [XML整形] k896951 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== 002.XML整形 ====== | ||
| + | 2026-04-28 コピペ用 | ||
| + | |||
| + | ===== 事前の知識 ===== | ||
| + | |||
| + | Chat-GPTさん情報で、PowerShell 5.1系はスクリプトを UTF-8 BOM付 で保存しておく。こうしておかないとスクリプトのパースに失敗する事があるそうだ。\\ ※今回 xmlupdate.ps1 で実際に喰らった | ||
| + | ===== XML整形 ===== | ||
| + | |||
| + | ==== powershell 5.x シリーズ用 ==== | ||
| + | |||
| + | <code powershell xmlformatter.ps1> | ||
| + | ## powershell -NoProfile -ExecutionPolicy Bypass -File xmlformatter.ps1 XMLファイル名 | ||
| + | |||
| + | param( | ||
| + | [Parameter(Mandatory = $true)] | ||
| + | [string] $InputPath | ||
| + | ) | ||
| + | |||
| + | if (Test-Path $InputPath) { | ||
| + | $dir = (Get-Location).Path | ||
| + | $base = [System.IO.Path]:: | ||
| + | $OutputPath = [System.IO.Path]:: | ||
| + | } else { | ||
| + | Write-Host " | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | ## 整形用にインデントを指示 | ||
| + | $settings = New-Object System.Xml.XmlWriterSettings | ||
| + | $settings.Indent = $true | ||
| + | |||
| + | ## XML読み込み | ||
| + | $xmldoc = New-Object System.Xml.XmlDocument | ||
| + | $xmldoc.Load($InputPath) | ||
| + | |||
| + | ## XML整形結果書き出し | ||
| + | $writer = [System.Xml.XmlWriter]:: | ||
| + | $xmldoc.Save($writer) | ||
| + | $writer.Close() | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== powershell 7.x シリーズ用 ==== | ||
| + | |||
| + | <code powershell xmlformatter.ps1> | ||
| + | ## pwsh -NoProfile -ExecutionPolicy Bypass -File xmlformatter.ps1 XMLファイル名 | ||
| + | |||
| + | param( | ||
| + | [Parameter(Mandatory = $true)] | ||
| + | [string] $InputPath | ||
| + | ) | ||
| + | |||
| + | Add-Type -AssemblyName System.Xml.Linq | ||
| + | |||
| + | if (Test-Path $InputPath) { | ||
| + | $dir = (Get-Location).Path | ||
| + | $base = [System.IO.Path]:: | ||
| + | $OutputPath = [System.IO.Path]:: | ||
| + | } else { | ||
| + | Write-Host " | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | ## 整形用にインデントを指示 | ||
| + | $settings = [System.Xml.Linq.SaveOptions]:: | ||
| + | |||
| + | ## XML読み込み | ||
| + | $xmldoc = [System.Xml.Linq.XDocument]:: | ||
| + | |||
| + | ## XML整形結果書き出し | ||
| + | $xmldoc.Save($OutputPath, | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 入力サンプルXML ==== | ||
| + | |||
| + | |||
| + | <code xml sample.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | < | ||
| + | <item id=" | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== 実行コマンドライン ==== | ||
| + | |||
| + | PowerShell 5.x系 | ||
| + | <code tt> | ||
| + | G: | ||
| + | </ | ||
| + | |||
| + | PowerShell 7.x系 | ||
| + | <code tt> | ||
| + | G: | ||
| + | </ | ||
| + | |||
| + | ==== 実行結果(整形結果) ==== | ||
| + | |||
| + | <code xml sample.new.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | < | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== XML更新 ===== | ||
| + | |||
| + | ==== powershell 5.x シリーズ用 ==== | ||
| + | |||
| + | <code powershell xmlupdate.ps1> | ||
| + | ## powershell -NoProfile -ExecutionPolicy Bypass -File xmlupdate.ps1 XMLファイル名 | ||
| + | |||
| + | param( | ||
| + | [Parameter(Mandatory = $true)] | ||
| + | [string] $InputPath | ||
| + | ) | ||
| + | |||
| + | if (Test-Path $InputPath) { | ||
| + | $dir = (Get-Location).Path | ||
| + | $base = [System.IO.Path]:: | ||
| + | $OutputPath = [System.IO.Path]:: | ||
| + | } else { | ||
| + | Write-Host " | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | ## 整形用にインデントを指示 | ||
| + | $settings = New-Object System.Xml.XmlWriterSettings | ||
| + | $settings.Indent = $true | ||
| + | |||
| + | ## XML読み込み | ||
| + | $xmldoc = New-Object System.Xml.XmlDocument | ||
| + | $xmldoc.Load($InputPath) | ||
| + | |||
| + | ## ノード修正 | ||
| + | $node = $xmldoc.SelectSingleNode("/ | ||
| + | if ($node -ne $null) { | ||
| + | $node.InnerText=" | ||
| + | $node.SetAttribute(" | ||
| + | } | ||
| + | |||
| + | ## ノード追加 | ||
| + | $node = $xmldoc.SelectSingleNode("/ | ||
| + | if ($node -ne $null) { | ||
| + | $itemdoc = $xmldoc.CreateElement(" | ||
| + | $itemdoc.InnerText=" | ||
| + | $itemdoc.SetAttribute(" | ||
| + | [void] $node.AppendChild($itemdoc) | ||
| + | } | ||
| + | |||
| + | ## 修正結果書き出し | ||
| + | $writer = [System.Xml.XmlWriter]:: | ||
| + | $xmldoc.Save($writer) | ||
| + | $writer.Close() | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== powershell 7.x シリーズ用 ==== | ||
| + | |||
| + | <code powershell xmlupdate.ps1> | ||
| + | ## pwsh -NoProfile -ExecutionPolicy Bypass -File xmlupdate.ps1 XMLファイル名 | ||
| + | |||
| + | using namespace System.Xml.Linq | ||
| + | using namespace System.Xml.XPath | ||
| + | |||
| + | param( | ||
| + | [Parameter(Mandatory = $true)] | ||
| + | [string] $InputPath | ||
| + | ) | ||
| + | |||
| + | if (Test-Path $InputPath) { | ||
| + | $dir = (Get-Location).Path | ||
| + | $base = [System.IO.Path]:: | ||
| + | $OutputPath = [System.IO.Path]:: | ||
| + | } else { | ||
| + | Write-Host " | ||
| + | exit | ||
| + | } | ||
| + | |||
| + | # SaveOptions の明示的生成 | ||
| + | $saveOptions = [System.Xml.Linq.SaveOptions]:: | ||
| + | |||
| + | ## XML読み込み | ||
| + | $xmldoc = [System.Xml.Linq.XDocument]:: | ||
| + | |||
| + | ## ノード修正 | ||
| + | $node = [System.Xml.XPath.Extensions]:: | ||
| + | if ($node -ne $null) { | ||
| + | $node.Value = " | ||
| + | $node.SetAttributeValue(" | ||
| + | } | ||
| + | |||
| + | ## ノード追加 | ||
| + | $node = [System.Xml.XPath.Extensions]:: | ||
| + | if ($node -ne $null) { | ||
| + | $itemdoc = [System.Xml.Linq.XElement]:: | ||
| + | $itemdoc.Value = " | ||
| + | $itemdoc.SetAttributeValue(" | ||
| + | [void] $node.Add($itemdoc) | ||
| + | } | ||
| + | |||
| + | ## 修正結果書き出し | ||
| + | $xmldoc.Save($OutputPath, | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 入力サンプルXML ==== | ||
| + | |||
| + | <code xml sample2.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | < | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== 実行コマンドライン ==== | ||
| + | |||
| + | PowerShell 5.x系 | ||
| + | |||
| + | <code tt> | ||
| + | G: | ||
| + | </ | ||
| + | |||
| + | PowerShell 7.x系 | ||
| + | |||
| + | <code tt> | ||
| + | G: | ||
| + | </ | ||
| + | |||
| + | ==== 実行結果(更新結果) ==== | ||
| + | |||
| + | <code xml sample2.new.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | < | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | <item id=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | {{tag> | ||
documents/proglang/powershell/powershell-008.txt · 最終更新: by k896951
