Data acquisition without use XML
All of technics{technical equipment} with which I have acquainted you, make search of the server in the form of the XML-document. There are other methods connected with XML, action of one of which can seem latent. Browsers do not analyze XML-documents and immediately create DOM-models, and it can reduce the speed at times necessary for Ajax-components, is especial if there is an analysis of the big documents by slow machines. An example is " search online " where results of search at once undertake from the server and are shown users as if they at once are printed in forms of search. For a component of such search important quickly to react to data input, but during too time it is necessary to analyze searches of the server quickly for a long time.
The latent processes - an important point in consideration of models, but the major reason for avoiding XML are inconvenient interfaces (API DOM) the client. Listing 5 shows a range, in which you need to get to receive value with help DOM, in case of several supported browsers.
Listing 5. Job with XML the document of search in JavaScript
// To find a name of the first thing in last turn of the buyer
var orderHistoryDoc = req.responseXML;
var orders = orderHistoryDoc.getElementsByTagName ("order");
var lastOrder = orders [orders.length - 1];
var firstItem = lastOrder.getElementsByTagName ("item") [0];
var itemNameElement = firstItem.firstChild;
var itemNameText = itemNameElement.firstChild.data;
Process more and more becomes complicated and there are some blanks between elements as each element firstChild constantly gets in some difficulty. JavaScript-libraries are applicable{applied} for simplification of job with XML-documents. They include Sarissa (see. Resources) and Google-ajaXSLT, both of which add functions Xpath to the majority of browsers.
Nevertheless it is necessary to reflect on alternatives. In addition to responseXML, XMLHttpRequest the object offers the property which is were called responseText which simply represents a body of the answer of the server as a line.
Property responseText
The method responseText is especially convenient, when it is necessary for server to send very simple values to the client, avoiding use of the big passband and overloads XML. For example the usual search true/false can be returned as the usual text, and also can be the simple list of names or the numbers divided{shared} by a point. Though usually it is better to not mix XML-searches and text searches in one application, use only one data format will make abstraction of a code and his{its} repeated use it is easier.
responseText it can be useful also in a combination with the data of XML-search. In the script where only it is necessary for you to receive a unique value from the document of search, in the best way "to swindle", addressing to XML as to a line of the text, instead of as to the structured document. For example listing 6 shows, how usual expressions can be used for first-order data acquisition in history Order of the buyer. This really "unusual" decision, in fact, basically, you should not rely on lexical performance of the XML-document.
Listing 6. Use of usual expression in object responseText from XMLHttpRequest
var orderHistoryText = req.responseText;
var matches = orderHistoryText.match (/<date> (. *?) </date>/);
var date = matches [1];
Use responseText in it ad-hoc understanding can be convenient under some circumstances. In an ideal, however, there could be a way of performance of the complex{difficult} and structured data in a format which easily would support JavaScript, and he would not have perenagruzok XML. Fortunately, such format exists.
Recording of objects in JavaScript (JSON)
Slightly opening a veil, we shall find out, that JavaScript-objects of the majority of applications will consist of the sociable files, the numbered files, lines, numbers and combinations of these types. As all types can be declared literally in JavaScript, probably statically define{determine} the circuit of objects in the unique way. Listing 7 declares object by means of syntax JSON and shows, how to it{him} to organize access. Square brackets designate numbering a file.
Listing 7. The announcement of simple object bukvenno in JavaScript with help JSON
var band = {
name: " The Beatles ",
members: [
{
name: "John",
instruments: ["Vocals", "Guitar", "Piano"]
},
{
name: "Paul",
instruments: ["Vocals", "Bass", "Piano", "Guitar"]
},
{
name: "George",
instruments: ["Guitar", "Vocals"]
},
{
name: "Ringo",
instruments: ["Drums", "Vocals"]
}
]
};
// We distinguish family of objects
var musician = band.members [3];
alert (musician.name
+ "played" + musician.instruments [0]
+ "with" + band.name);
Thus, JSON - interesting feature of language, but what she does{makes} with Ajax? Essence that you can use JSON for departure of the JavaScript-circuit of object on a network in Ajax-search of the server. It will allow to avoid processing XML in the client with use of bulky interface DOM API, - you only should process JSON-search, and you at once receive the accessible circuit of JavaScript-object. First, however, transformation of yours JavaBeans in JSON can be necessary for you.

|