xml 的 REST 表示
我正在构建一个 REST api 并讨论应该使用什么格式来表示 xml。 我知道它可以是内部定义的,但也有很多格式。 wadl 看起来很有趣,但似乎存在争议,而且它也是新的。
有哪些建议(最佳实践)?
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 well.
what are the recommendations(best practices for it)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要它是有效的 XML,那就是一个开始。我的简单 REST/XML 服务的一般策略如下:
对于解析,您几乎肯定需要一个始终存在的根节点,例如“响应”,它可以包含结果元素或错误元素。
例如:
构建结果的方式取决于您。您可以决定是否将 elemenets 中的数据保存为 CDATA(文本注释,如上所示)或作为必需属性,例如
有时有用的另一件事是在响应中包含原始查询,例如:
这样,如果您有多个查询未解决,您可以将它们与原始请求匹配。
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.:
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.
Another thing that is sometimes useful is to include your original query in the response, e.g.:
That way if you have multiple queries outstanding you can match them back to the original request.