全部1行に収まったxmlファイルをもらったのですが、そのままでは扱いにくくてしょうが無いので、インデントするスクリプトを作りました。
下記スクリプトをhoge.ps1に保存して、powershellで実行します。
オプションの説明は get-help hoge.ps1 で確認できます。
<#
.SYNOPSIS
xmlファイルをインデントするスクリプト
.DESCRIPTION
xmlファイルを読み込んで、インデント処理してxmlファイルに出力します。
入力・出力ファイルともに、エンコードはUTF8固定です。
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]
#入力するXMLファイルへのパス
$inputXmlPath,
[string]
#出力するXMLファイルへのパス。省略した場合、入力XMLファイルパスに"indented_"プリフィックスをつけた値を使用します。
$outputXmlPath
)
function main(){
#入力XMLファイルパスを絶対パスに変換&ファイル存在チェック
$absoluteInputXmlPath = Convert-Path $inputXmlPath 2> $null
if($? -eq $false){
Write-Output "入力XMLファイル[$inputXmlPath]が存在しません。処理を中断します。"
exit -1
}
#出力XMLファイルパスを絶対パスに変換&パス存在チェック
if( ($outputXmlPath -eq $null) -or ($outputXmlPath -eq "")){
$absoluteOutputXmlPath = Join-Path (Split-Path $absoluteInputXmlPath -Parent) ( "indented_" + (Split-Path $absoluteInputXmlPath -Leaf) )
}else{
$outputFolderPath = Split-Path $outputXmlPath -Parent
$absoluteOutputFolderPath = Convert-Path $outputFolderPath 2> $null
if($? -eq $false){
Write-Output "出力先フォルダ[$outputFolderPath]が存在しません。処理を中断します。"
exit -1
}
$absoluteOutputXmlPath = Join-Path $absoluteOutputFolderPath (Split-Path $outputXmlPath -Leaf)
}
#xmlファイルを読み込んでインデントして出力する
$xmlDoc = [System.Xml.XmlDocument](Get-Content $absoluteInputXmlPath -Encoding utf8)
$encoding = [System.Text.Encoding]::UTF8
$xmlWriter = New-Object System.Xml.XmlTextWriter($absoluteOutputXmlPath, $encoding)
$xmlWriter.Formatting = [System.Xml.Formatting]::Indented
try{
$xmlDoc.Save($xmlWriter)
if($? -eq $false){
Write-Output "処理失敗しました。"
exit -1
}else{
Write-Output "処理完了しました。"
Write-Output "出力先[$absoluteOutputXmlPath]"
}
}catch [System.Exception]{
Write-Output "処理中にExceptionが発生しました。"
$Error
exit -1
}finally{
$xmlWriter.Close()
}
}
#エントリーポイント
Write-Output "起動パラメータ:"
$PSBoundParameters.Keys | %{Write-Output(" " + $_ + " = " + $PSBoundParameters[$_])}
main