【问题标题】:curl and xmllint pipecurl 和 xmllint 管道
【发布时间】:2013-11-23 09:32:51
【问题描述】:

我尝试使用管道 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'

【问题讨论】:

  • 也许它打印到 stderr 并且您需要2>&1?或者你可以使用我的xidel http://www.test.com -e //title 然后你不需要管道

标签: xml curl xmllint


【解决方案1】:

更简洁

curl foo.com/somefile.xml | xmllint --format -

解释:

这里我们将piping 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 - ).

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

【讨论】:

  • 我添加了一个解释。尽管我想提供一个简短的答案。
  • 也可以在本地保存文件curl foo.com/file.xml | xmllint --format - > file.xml谢谢
【解决方案2】:

似乎xmllint 要求- stdin 重定向位于命令末尾。

curl --location --header "Accept: application/rdf+xml" http://www.test.com \
  | xmllint --format --xpath '//title' -

【讨论】:

  • 如何用换行符分隔结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2019-08-23
  • 1970-01-01
  • 2012-09-17
  • 2020-05-08
相关资源
最近更新 更多