tags:

views:

36

answers:

2

Is there a way to tell if a feed is XML or JSON or both? thnx

+1  A: 

The best and sure-fire way would be simply to run it through a XML and a JSON parser, and see which one works without generating syntax errors. For example, in PHP, try json_encode($feed_string) and $xml = new SimpleXMLElement($feed_string);

Alternatively, you can just do some simple string checking. All properly-formed XML documents start with <?xml, while JSON typically starts with { since the feed data is a Javascript object.

The samples Google provides as the two feed formats may be helpful:

http://code.google.com/apis/gdata/docs/json.html

phsource
ahh...thank you!
bresson
+2  A: 

I'm not really clear on what you mean by 'feed', but if the mime type of a file is set to application/json, then it is JSON. XML has two standard mime types (application/xml and text/xml).

If you don't have access to the mime types (or they are ambiguous), you can check for <?xml at the start of a proper xml file. And if that isn't there, then you can probably do a pretty good guess that it is XML if it starts with just < and JSON if it starts with {. But there is no guarantee they will be correctly formed.

RodeoClown
sorry rodeoclown but i forgot to add RSS feed in the body. my impressions were XML and JSON were the two main standards for delivering RSS
bresson
No worries. RSS is XML by definition. See http://stackoverflow.com/questions/246577/can-i-serve-rss-in-json for some other people who have used JSON to create an RSS-like feed.
RodeoClown