开发者

REST representation for xml

I'm building a REST api and debating what format should I use for the xml representation. I know it can be one defined in house but there are also lots for formats. wadl looks interesting but there seems to be a debate about it and it's new as w开发者_运维知识库ell.

what are the recommendations(best practices for it)?


As long as it's valid XML that's a start. My general strategy for simple REST/XML services is as follows:

For parsing, you almost certainly want a single, always present root node, e.g. "response", which can contain either a result element or an error element. E.g.:

<response>
  <result>
    <answer>5</answer>
  </result>
</response>

The way you structure the results is up to you. You can decide whether to hold data in elemenets as CDATA (text notes, as show above) or as required attributes, e.g.

<answer value="5"/>

Another thing that is sometimes useful is to include your original query in the response, e.g.:

<response query="operation=add&a=2&b=3"> .. </response>

That way if you have multiple queries outstanding you can match them back to the original request.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜