开发者

How do I determine if an RSS feed is XML or JSON?

Is there a way to tell if a feed开发者_如何学编程 is XML, JSON, or both?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜