5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでXMLが解釈可能か(パースできるか)確認する

Posted at

ちゃんとプロダクトを作るときなどは、XML Schemaなどを書いてバリデータを通すべきだが、手元でテスト用のデータをパッと確認したいようなときがある。
PowerShellでワンライナーで確認できるので、古すぎないWin機があれば、特別な開発環境がなくてもXMLがパース可能かすぐに確認できる。

XMLファイルのパース

 [xml]$xml = cat hoge.xml -Encoding UTF8

でエラーメッセージが表示されなければOK。
catコマンド( = Get-Content コマンドのエイリアス)の文字エンコードはリトルエンディアンのUTF16なので、それ以外の場合は適切にエンコーディングを指定すると動作する。

1行1XMLで複数行のXMLがある場合

Hadoopの出力などで、1ファイルの中に1行づつXMLが入っているような場合は、catの出力をパイプで渡してfor-eachにかけるとよい。

 cat hoge.xml -Encoding UTF8 | %{[xml]$_}

複数ファイルの場合も

 cat directory/*.xml -Encoding UTF8 | %{[xml]$_}

でエラーがないかチェックするとよい。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?