2

I am issuing the following command:

curl http://localhost:8082/some-url

Which returns xml that looks similar to this:

<?xml version="1.0" encoding="UTF-8"?><user><dateCreated>1</dateCreated><dateLastModified>1</dateLastModified><id>1</id><disabled>false</disabled><firstName></firstName><lastName></lastName></user>

What is the easiest way to extract an xml element from the response, e.g. the element "true"?

Note, I want the complete opening tag, content, and closing tag (and the content may also be empty.

Thanks

user2586917
  • 762
  • 1
  • 9
  • 26

4 Answers4

4

Using xml_grep, which comes with XML::Twig, you can do curl http://localhost:8082/some-url | xml_grep --nowrap disabled (I assumed you wanted the content of the disabled element, I don't see any true element in your data)

mirod
  • 15,923
  • 3
  • 45
  • 65
3

Would xmllint be an option for you as well?

$ curl -s http://localhost:8082/some-url | xmllint --xpath '//user/disabled' -
Kokkie
  • 546
  • 6
  • 15
0

Or the same with xsh (http://xsh.sourceforge.net/):

$ curl -s http://localhost:8082/some-url | xsh -q -I - -C 'ls //user/disabled/text()'
buff
  • 2,063
  • 10
  • 16
0

The classic solution is to use xmlstarlet, for example for an RSS feed:

URL="https://feeds.feedburner.com/TEDenEspanol"
curl -s $URL | xmlstarlet sel -t -v 'rss/channel/title'
Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30