curl and xmllint pipe
Asked Answered
G

2

40

I try to pipe curl and xmllint to parse the xml output from an url. But for some reason xml won't parse the xml and instead the resulting xml from the curl is shown. I'm a missing a setting? If is store the result of the curl action as a file and use that as an input for xmllint it parses correctly.

 curl --location --header "Accept: application/rdf+xml" http://www.test.com | xmllint --format - --xpath '//title'
Gazette answered 11/11, 2013 at 14:28 Comment(1)
Perhaps it prints to stderr and you need 2>&1? Or you could use my xidel http://www.test.com -e //title then you do not need to pipeAtelectasis
D
67

Seems that xmllint requires the - stdin redirect be at the end of the command.

curl --location --header "Accept: application/rdf+xml" http://www.test.com \
  | xmllint --format --xpath '//title' -
Dig answered 4/1, 2014 at 23:57 Comment(2)
how to separate results with newlineKaterine
This is almost a convention to use - to represent a file read from stdin.Dubuffet
C
13

more succinct

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

Explanation:

Here we are piping the xml from the curl command into the xmllint command. The xmllint man page says

$ 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 - ).

So that's why we do xmllint --format - because this particular command will read from stdin if you specify - as the filename. Sidenote, there's a discussion about the - arg here. I personally don't like that stdin is not the default but I'm not the author.

Cryptology answered 3/8, 2020 at 20:10 Comment(2)
I added an explanation. Even though I wanted to provided a short answer.Cryptology
Also save file locally curl foo.com/file.xml | xmllint --format - > file.xml thanksPredominate

© 2022 - 2024 — McMap. All rights reserved.