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!

[email protected]:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697

170922Peter Kiewit Institute79037College & Education:UniversityUniversityhttp://foursquare.com/img/categories/education/default.png
1110 South 67th Street
at Pacific StOmahaNE6818241.2436984-96.014670204025543333PKI255
345883Liv Lounge79168Nightlife:LoungeLoungehttp://foursquare.com/img/categories/nightlife/lounge.png
2279 S 67th St
at Center StOmahaNebraska6810641.2436984-96.01467020255
245578Walmart79221Shops:Department StoreDepartment Storehttp://foursquare.com/img/categories/shops/default.png
1606 S 72nd St
at Pine StOmahaNE6812441.2440158-96.024115804023939560541
254875Aksarben Village79196Parks & Outdoors:Plaza / SquarePlaza / Squarehttp://foursquare.com/img/categories/parks_outdoors/default.png
6600 Shirley St
at Center StOmahaNE6810641.2414632-96.01205190518
170916Scott Technology Center79115Home / Work / Other:Corporate / OfficeCorporate / Officehttp://foursquare.com/img/categories/building/default.png
6825 Pine Street
OmahaNE6810641.243385-96.017697040250260000
2468550Juice Stop79071Food:Juice BarJuice Barhttp://foursquare.com/img/categories/food/default.png
2119 S 67th St
Shirley StOmahaNE6810641.2403021-96.014660704029343870427
199022Scott Conference Center96353Home / Work / Other:Event SpaceEvent Spacehttp://foursquare.com/img/categories/building/default.png
6450 Pine Street
OmahaNE41.244514-96.0134890373
772128Courtyard Aksarben VIllage79273Travel:HotelHotelhttp://foursquare.com/img/categories/travel/hotel.png
1625 S 67th St
at Pine StOmahaNE6810641.243304-96.01434904029514300280
247344Wohlner's Market96359Shops:Food & Drink:GourmetGourmethttp://foursquare.com/img/categories/shops/default.png
2289 S 67th St
at Center StOmahaNE6810641.23806024488222-96.013898849487304025516875672
2564306First Data79115Home / Work / Other:Corporate / OfficeCorporate / Officehttp://foursquare.com/img/categories/building/default.png
6855 Pacific Street
OmahaNE6810641.2487029-96.01925530606
[email protected]:~$

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

[email protected]:~$ 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


  
    
      170922
      Peter Kiewit Institute
      
        79037
        College & Education:University
        University
        http://foursquare.com/img/categories/education/default.png
      
      
1110 South 67th Street
at Pacific St Omaha NE 68182 41.2436984 -96.0146702 0 4025543333 PKI 255
... snip ... 2564306 First Data 79115 Home / Work / Other:Corporate / Office Corporate / Office http://foursquare.com/img/categories/building/default.png
6855 Pacific Street
Omaha NE 68106 41.2487029 -96.0192553 0 606
[email protected]:~$

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.