• Post Reply Bookmark Topic Watch Topic
  • New Topic

REST vs HTML  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the advantage of using REST (sending XML over HTTP) vs HTML?

My confusion is, I have got data in java objects. Now I have two choices, either I can prepare HTML at server side and send it back to client or I can prepare XML at server side and send it back to client, and browser will parse this XML or use XSL to generate view...

What are the pros and cons of each approach?

Thanks.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML over HTTP isn't necessarily REST. The representation of a resource that you are accessing may be represented in XML but it could be represented in another way.
Richardson & Ruby in their book RESTful Web Services (amazon US) actually choose HTML 5 as the representation format for their examples of the resource/representation design process.

And there are instances of XML over HTTP that aren't considered RESTful.

HTML marks up the data for appearance (for human readability) - that however doesn't help machines (non-browser programs) that are trying to interpret the data in the page. XML creates an opportunity to give the data more "meaning" by marking it up with "meta-data" (data about data) - this meta-data, in theory, helps non-browser programs isolate the information that they are looking for. So XML over HTTP is mainly intended for machine consumption it is not supposed to be used by a browser (unless it feeds a JavaScript XMLHttpRequest object - now we are getting AJAX).

Depending on the circumstances it may be useful on the server side to represent your page data in XML (with its meta-data) and then on the server side use XSLT to generate an HTML page for a browser. The XML data is usually programmatically generated while the XSLT template is static - it converts the XML data to an HTML view (which can be further customized via CSS). Using XSLT on the client side browser never really caught on. However since the emergence of AJAX, XML over HTTP is often consumed through the JavaScript XMLHttpRequest object.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peer Reynders:
XML over HTTP isn't necessarily REST. The representation of a resource that you are accessing may be represented in XML but it could be represented in another way.
Richardson & Ruby in their book RESTful Web Services (amazon US) actually choose HTML 5 as the representation format for their examples of the resource/representation design process.

And there are instances of XML over HTTP that aren't considered RESTful.

HTML marks up the data for appearance (for human readability) - that however doesn't help machines (non-browser programs) that are trying to interpret the data in the page. XML creates an opportunity to give the data more "meaning" by marking it up with "meta-data" (data about data) - this meta-data, in theory, helps non-browser programs isolate the information that they are looking for. So XML over HTTP is mainly intended for machine consumption it is not supposed to be used by a browser (unless it feeds a JavaScript XMLHttpRequest object - now we are getting AJAX).

Depending on the circumstances it may be useful on the server side to represent your page data in XML (with its meta-data) and then on the server side use XSLT to generate an HTML page for a browser. The XML data is usually programmatically generated while the XSLT template is static - it converts the XML data to an HTML view (which can be further customized via CSS). Using XSLT on the client side browser never really caught on. However since the emergence of AJAX, XML over HTTP is often consumed through the JavaScript XMLHttpRequest object.


Thanks Peer for excellent information.

Now, let me correct my question.

Sending XML over HTTP and consuming it through XMLHttpRequest (making it specific to browser) vs sending HTML. What are the pros and cons?

Thanks a ton.

May be, it's not in correct forum now...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sending XML over HTTP and consuming it through XMLHttpRequest (making it specific to browser) vs sending HTML. What are the pros and cons?


HMLHttpRequest requres code on the browser side to control the display. Probably slower, increases the size of the response and potentially introduces browser dependencies

HTML will be more compact, faster and should have less browser dependency.

Bill
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
XMLHttpRequest requires code on the browser side to control the display. Probably slower, increases the size of the response and potentially introduces browser dependencies


And requires additional requests to load the full page. Even if you are willing to tolerate all that, XML may not even be the best choice, especially if the request will always come from browser-based JavaScript.

JavaScript: The Definitive Guide, 5e amazon US p.489-490.
Note however that using XML as a data format may not always be the best choice. If the server wants to pass data to be manipulated by a JavaScript script, it is inefficient to encode that data into XML form on the server, have the XMLHttpRequest object parse that data to a tree of DOM nodes, and then have your script traverse that tree to extract data. A shorter path is to have the server encode the data using JavaScript object and array literals and pass the JavaScript source text to the web browser. The script then "parses" the response simply by passing it to the JavaScript eval() method.
Encoding data in the form of JavaScript object and array literals is known as JSON, or JavaScript Object Notation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!