卷曲和xmllint管道

Tho*_*mas 22 xml curl xmllint

我尝试管道curl和xmllint来解析url的xml输出.但由于某种原因,xml不会解析xml,而是会显示curl生成的xml.我错过了一个场景?如果将curl操作的结果存储为文件并将其用作xmllint的输入,则会正确解析.

 curl --location --header "Accept: application/rdf+xml" http://www.test.com | xmllint --format - --xpath '//title'
Run Code Online (Sandbox Code Playgroud)

小智 46

似乎xmllint需要-stdin重定向在命令的末尾.

curl --location --header "Accept: application/rdf+xml" http://www.test.com \
  | xmllint --format --xpath '//title' -
Run Code Online (Sandbox Code Playgroud)


Har*_*eno 12

更简洁

curl foo.com/somefile.xml | xmllint --format -
Run Code Online (Sandbox Code Playgroud)

解释:

在这里,我们将xml 从 curl 命令传输到 xmllint 命令中。xmllint 手册页说

$ man xmllint
> ... The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is - ).
Run Code Online (Sandbox Code Playgroud)

这就是我们这样做的原因,因为如果您指定为文件名,xmllint --format -则此特定命令将从 stdin 读取。旁注,这里-有关于-arg 的讨论。我个人不喜欢 stdin 不是默认值,但我不是作者。

  • 还可以在本地保存文件 `curl foo.com/file.xml | xmllint --format -> file.xml` 谢谢 (2认同)