xmllint (系統(tǒng)自帶的指令)
如果您輸入的XML包含前綴的名稱空間颓芭,則必須使用local-name()代替name()嬉探。
CentOS
使用--xpath
的時候會有問題,可以選擇--shell
# xmllint --xpath "/" pom.xml
Unknown option --xpath
Usage : xmllint [options] XMLfiles ...
Parse the XML files and output the result of the parsing
--version : display the version of the XML library used
--debug : dump a debug tree of the in-memory document
......
使用--shell
(CentOS和MacOS通用)
# xmllint --shell pom.xml
/ > xpath //*[name() = 'project']/*[name() = 'version']/text()
Object is a Node Set :
Set contains 1 nodes:
1 TEXT
content=1.1.5-SNAPSHOT
/ >
Mac
使用--xpath
(在CentOS6.5中糜烹,由于libxml2的版本過低娶靡,所以無法直接使用--xpath)
okami$ xmllint --xpath "http://*[name() = 'project']/*[name() = 'version']/text()" pom.xml
1.1.5-SNAPSHOT
okami$ xmllint --shell pom.xml
/ > xpath //*[name() = 'project']/*[name() = 'version']/text()
Object is a Node Set :
Set contains 1 nodes:
1 TEXT
content=1.1.5-SNAPSHOT
/ >
xpath
xpath目前只發(fā)現(xiàn)可以在MacOS上使用茎芭,CentOS上暫時沒有找到該工具
okami$ xpath pom.xml "/project/version/text()"
Found 1 nodes:
-- NODE --
1.1.5-SNAPSHOT
xml2
okami$ cat pom.xml |xml2 |grep /project/version
/project/version=1.1.5-SNAPSHOT
xmlstarlet
安裝 xmlstarlet
# yum install xmlstarlet -y
okami$ brew install xmlstarlet
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
bit exploitdb go@1.12 minikube navi pandoc stress-ng terrahub unbound wxmaxima
convox gnome-autoar mesa msgpack paket powerline-go sysbench traefik unrar
==> Downloading https://homebrew.bintray.com/bottles/xmlstarlet-1.6.1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring xmlstarlet-1.6.1.mojave.bottle.tar.gz
?? /usr/local/Cellar/xmlstarlet/1.6.1: 15 files, 193.2KB
使用 xmlstarlet
# xmlstarlet sel -t -v "http://*[name() = 'project']/*[name() = 'version']/text()" pom.xml
1.0-SNAPSHOT
參考鏈接
最后編輯于 :2019.12.14 15:23:46
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者