Is there a way to tell if a feed is XML or JSON or both? thnx
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:
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.