Use xmllint to Pretty Print API Output

June 10, 2010

I’ve been messing with the Foursquare API and I was getting frustrated by it’s unreadable output. Too much XML, not enough whitespace!

jmhobbs@katya:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697
<?xml version="1.0" encoding="UTF-8"?>
<venues><group type="Nearby"><venue><id>170922</id><name>Peter Kiewit Institute</name><primarycategory><id>79037</id><fullpathname>College &amp; Education:University</fullpathname><nodename>University</nodename><iconurl>http://foursquare.com/img/categories/education/default.png</iconurl></primarycategory><address>1110 South 67th Street</address><crossstreet>at Pacific St</crossstreet><city>Omaha</city><state>NE</state><zip>68182</zip><geolat>41.2436984</geolat><geolong>-96.0146702</geolong><stats><herenow>0</herenow></stats><phone>4025543333</phone><twitter>PKI</twitter><distance>255</distance></venue><venue><id>345883</id><name>Liv Lounge</name><primarycategory><id>79168</id><fullpathname>Nightlife:Lounge</fullpathname><nodename>Lounge</nodename><iconurl>http://foursquare.com/img/categories/nightlife/lounge.png</iconurl></primarycategory><address>2279 S 67th St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>Nebraska</state><zip>68106</zip><geolat>41.2436984</geolat><geolong>-96.0146702</geolong><stats><herenow>0</herenow></stats><distance>255</distance></venue><venue><id>245578</id><name>Walmart</name><primarycategory><id>79221</id><fullpathname>Shops:Department Store</fullpathname><nodename>Department Store</nodename><iconurl>http://foursquare.com/img/categories/shops/default.png</iconurl></primarycategory><address>1606 S 72nd St</address><crossstreet>at Pine St</crossstreet><city>Omaha</city><state>NE</state><zip>68124</zip><geolat>41.2440158</geolat><geolong>-96.0241158</geolong><stats><herenow>0</herenow></stats><phone>4023939560</phone><distance>541</distance></venue><venue><id>254875</id><name>Aksarben Village</name><primarycategory><id>79196</id><fullpathname>Parks &amp; Outdoors:Plaza / Square</fullpathname><nodename>Plaza / Square</nodename><iconurl>http://foursquare.com/img/categories/parks_outdoors/default.png</iconurl></primarycategory><address>6600 Shirley St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2414632</geolat><geolong>-96.0120519</geolong><stats><herenow>0</herenow></stats><distance>518</distance></venue><venue><id>170916</id><name>Scott Technology Center</name><primarycategory><id>79115</id><fullpathname>Home / Work / Other:Corporate / Office</fullpathname><nodename>Corporate / Office</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6825 Pine Street</address><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.243385</geolat><geolong>-96.017697</geolong><stats><herenow>0</herenow></stats><phone>4025026000</phone><distance>0</distance></venue><venue><id>2468550</id><name>Juice Stop</name><primarycategory><id>79071</id><fullpathname>Food:Juice Bar</fullpathname><nodename>Juice Bar</nodename><iconurl>http://foursquare.com/img/categories/food/default.png</iconurl></primarycategory><address>2119 S 67th St</address><crossstreet>Shirley St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2403021</geolat><geolong>-96.0146607</geolong><stats><herenow>0</herenow></stats><phone>4029343870</phone><distance>427</distance></venue><venue><id>199022</id><name>Scott Conference Center</name><primarycategory><id>96353</id><fullpathname>Home / Work / Other:Event Space</fullpathname><nodename>Event Space</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6450 Pine Street</address><city>Omaha</city><state>NE</state><geolat>41.244514</geolat><geolong>-96.013489</geolong><stats><herenow>0</herenow></stats><distance>373</distance></venue><venue><id>772128</id><name>Courtyard Aksarben VIllage</name><primarycategory><id>79273</id><fullpathname>Travel:Hotel</fullpathname><nodename>Hotel</nodename><iconurl>http://foursquare.com/img/categories/travel/hotel.png</iconurl></primarycategory><address>1625 S 67th St</address><crossstreet>at Pine St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.243304</geolat><geolong>-96.014349</geolong><stats><herenow>0</herenow></stats><phone>4029514300</phone><distance>280</distance></venue><venue><id>247344</id><name>Wohlner's Market</name><primarycategory><id>96359</id><fullpathname>Shops:Food &amp; Drink:Gourmet</fullpathname><nodename>Gourmet</nodename><iconurl>http://foursquare.com/img/categories/shops/default.png</iconurl></primarycategory><address>2289 S 67th St</address><crossstreet>at Center St</crossstreet><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.23806024488222</geolat><geolong>-96.0138988494873</geolong><stats><herenow>0</herenow></stats><phone>4025516875</phone><distance>672</distance></venue><venue><id>2564306</id><name>First Data</name><primarycategory><id>79115</id><fullpathname>Home / Work / Other:Corporate / Office</fullpathname><nodename>Corporate / Office</nodename><iconurl>http://foursquare.com/img/categories/building/default.png</iconurl></primarycategory><address>6855 Pacific Street</address><city>Omaha</city><state>NE</state><zip>68106</zip><geolat>41.2487029</geolat><geolong>-96.0192553</geolong><stats><herenow>0</herenow></stats><distance>606</distance></venue></group></venues>
jmhobbs@katya:~$

How can we rectify this situation? xmllint! This is part of libxml and should exist on most systems.

jmhobbs@katya:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697 | xmllint --format -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
101  5265  101  5265    0     0  13778      0 --:--:-- --:--:-- --:--:-- 29914
<?xml version="1.0" encoding="UTF-8"?>
<venues>
  <group type="Nearby">
    <venue>
      <id>170922</id>
      <name>Peter Kiewit Institute</name>
      <primarycategory>
        <id>79037</id>
        <fullpathname>College &amp; Education:University</fullpathname>
        <nodename>University</nodename>
        <iconurl>http://foursquare.com/img/categories/education/default.png</iconurl>
      </primarycategory>
      <address>1110 South 67th Street</address>
      <crossstreet>at Pacific St</crossstreet>
      <city>Omaha</city>
      <state>NE</state>
      <zip>68182</zip>
      <geolat>41.2436984</geolat>
      <geolong>-96.0146702</geolong>
      <stats>
        <herenow>0</herenow>
      </stats>
      <phone>4025543333</phone>
      <twitter>PKI</twitter>
      <distance>255</distance>
    </venue>
... snip ...
    <venue>
      <id>2564306</id>
      <name>First Data</name>
      <primarycategory>
        <id>79115</id>
        <fullpathname>Home / Work / Other:Corporate / Office</fullpathname>
        <nodename>Corporate / Office</nodename>
        <iconurl>http://foursquare.com/img/categories/building/default.png</iconurl>
      </primarycategory>
      <address>6855 Pacific Street</address>
      <city>Omaha</city>
      <state>NE</state>
      <zip>68106</zip>
      <geolat>41.2487029</geolat>
      <geolong>-96.0192553</geolong>
      <stats>
        <herenow>0</herenow>
      </stats>
      <distance>606</distance>
    </venue>
  </group>
</venues>
jmhobbs@katya:~$

So there you go, a handy trick of the day for you.
In case you missed it the short form is:
curl -l [url] | xmllint --format -

Categories: Geek
Tags: , , , ,

Comments

  1. If you don’t mind me asking, why are you using the XML API instead of the JSON API? I’ve all but abandoned XML for just about everything. It’s an absolute pain to work with.

  2. john says:

    I’m not using it in production, but I like it for reading in a shell. Visually I don’t grok JSON as fast as I do XML, but that may just be me.

  3. john says:

    As for working with it, SimpleXML (http://www.php.net/simple_xml) in PHP is really great for small documents. And if you are handling RSS or ATOM then SimplePie (http://simplepie.org/) is an awesome all in one solution.

    Likewise in Python lxml (http://codespeak.net/lxml/) and Universal Feed Parser (http://www.feedparser.org/)

Leave A Comment

Your email will not be published.

*