LoginSignup
1
1

xmllintでxmlタグの値を取得

Last updated at Posted at 2023-11-18

xmllintでタグの値を取得したい場合、このようにワンライナーで取得できる。

echo 'cat /note/body' | xmllint --shell /home/vpsuser/conf/sample.xml | grep body | sed 's/^.*<body.*>\(.*\)<\/body>.*/\1/'
実行結果
Don't forget me this weekend!

サンプルxmlの内容は下記の通り。(今回はDon't forget me this weekend!を取得)

sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

※備考
・このcatは、xmllintの内部コマンドであり、通常のLinuxコマンドのcatとは別物である。
・xmllintの--shell オプションは標準入力からxmllintの内部コマンド(この場合はcat)を読んで実行する。

shellscriptで実行するなら下記

value=`echo 'cat /note/body' | xmllint --shell /home/vpsuser/conf/sample.xml | grep body | sed 's/^.*<body.*>\(.*\)<\/body>.*/\1/'`
echo $value
1
1
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
1
1