Transfer XML on the server
When business will reach transfer XML on the server, you will spend more codings to take the data and to wrap up them in format XML, than for actual data transfer. Actually, as soon as your XML-line is ready for transfer not the server, you send her{it} precisely how you would send any other plain text; check up Listing 2 to see it in operation.
Listing 2. Sending of pairs name / value in XML
function callServer () {
// Get the city and state from the Web form
var firstName = document.getElementById ("firstName") .value;
var lastName = document.getElementById ("lastName") .value;
var street = document.getElementById ("street") .value;
var city = document.getElementById ("city") .value;
var state = document.getElementById ("state") .value;
var zipCode = document.getElementById ("zipCode") .value;
var xmlString = "<profile>" +
"<firstName>" + escape (firstName) + "</firstName>" +
"<lastName>" + escape (lastName) + "</lastName>" +
"<street>" + escape (street) + "</street>" +
"<city>" + escape (city) + "</city>" +
"<state>" + escape (state) + "</state>" +
"<zip-code>" + escape (zipCode) + "</zip-code>" +
"</profile>";
// We shall construct URL for connection
var url = "/scripts/saveAddress.php";
// We shall open connection with the server
xmlHttp.open ("POST", url, true);
// We inform the server, that you send the data in format XML
xmlHttp.setRequestHeader ("Content-Type", "text/xml");
// We shall establish function of start of the server when it is executed
xmlHttp.onreadystatechange = confirmUpdate;
// We shall send the order
xmlHttp.send (xmlString);
}
The majority of it is understandable without explanations, some moments should be noted. First, the data in your search should be manually formatted as XML. It is a small step back after three clauses{articles} on use Document Object Model, whether not so? And as nothing forbids to you to apply DOM to creation of the XML-document with help JavaScript, you then should translate that DOM-object in the text before to send it{him} on a network with GET or POST-search. Therefore it appears to format simply more easy the data with the help of usual line manipulation. Certainly, it creates an opportunity of an assumption of mistakes or typing errors, therefore you need to be especially close{attentive} at recording a code, in attitude{relation} XML.
As soon as you have designed yours XML, you open connection in the greater degree in the same way, as at transfer of the text. I prefer to apply POST-searches for XML as some browsers impose restrictions on extent on chains of GET-search, and XML can become rather long; you will see, that Listing 2 is switched from GET to POST accordingly. Besides, XML it is preferably sent by means of a method send (), instead of as the parameter attached on end URL which you request. All rather trivial distinctions to which it is easy to adapt also consist in it.
To you, the truth, is necessary to write down completely new line of a code:
xmlHttp.setRequestHeader ("Content-Type", "text/xml");
She{It} is uneasy for understanding: she simply informs the server, that you send her{it} as XML, instead of as simple old pairs name/value. In any case, you send the data as the text, but apply here text/xml, or XML, sent as the usual text. If you simply used pairs name/value, the line will be such:
xmlHttp.setRequestHeader ("Content-Type", "text/plain");
If you have forgotten to inform the server, that you send her{it} as XML, you appear in difficulty, therefore do not forget this step.
As soon as all of you will make it, everything, that you will need to make, it to call send () and to pass it{him} in XML-line. The server will receive your XML-search, and (assuming, that you vypolnli all preliminary job) signs XML, will decipher it{him} and will be sent you with the answer. That's all, that is necessary to make - XML-searches only with several changes in a code.
Sending XML: pro and contra?
Before you will leave XML-searches (and this clause{article}) and will pass to XML-answers, let's reflect, as far as it is reasonable to apply XML in your searches. I already mentioned, that XML cannot be considered as the most high-speed data format concerning transfer in any way, but besides it is above what to think.
Not simply to design XML
The first, that you need to understand, it that XML not so simply to design for application in searches. As you saw in Listing 2, you dannnye quickly become too intricate because of semantics XML:
var xmlString = "<profile>" +
"<firstName>" + escape (firstName) + "</firstName>" +
"<lastName>" + escape (lastName) + "</lastName>" +
"<street>" + escape (street) + "</street>" +
"<city>" + escape (city) + "</city>" +
"<state>" + escape (state) + "</state>" +
"<zip-code>" + escape (zipCode) + "</zip-code>" +
"</profile>";
Can seem, that in it there is nothing bad, but it is a XML-fragment which has only six fields. The majority of Web-forms which you will develop, will have from ten up to fifteen; though you will not apply Ajax to all your searches, above it it is necessary to reflect. You spend, at least, as much time, being engaged in angular brackets and names tegov, how much you spend for the valid data, and the opportunity to make a typing error - is huge.
There is here one more problem - as it was already marked - that to you is necessary to design this XML manually. Application DOM is not a good variant as there are no good simple ways to turn DOM-object at line which you can send as search. Therefore job with similar lines is really the best variant, but it also a variant which it is the most difficult to accompany (to serve) and most difficultly to understand to new developers. In it sduchae you have designed all XML in one line; all becomes much more confusing, when you do{make} it in some steps.
XML does not add anything to your searches
Besides a problem of a complexity, application XML for your searches actually does not offer you the big advantages - if at all they are - in comparison with the usual text and pairs name/value. Mean, that all in this clause{article} is aimed at a capture of the same data which you could send already, applying pairs name / value (address to Listing 1) and their transfer with help XML. Anywhere it was spoken nothing about the data which it is possible otpraljat` with help XML and which could not be sent, using the usual text; it because almost never no anything, that it is possible to send with help XML that it would be impossible to send, using the usual text.
, actually, and everything, as to XML and searches: in their advantage{benefit} rather seldom there are sensible arguments. In following clause{article} of this series you will see, that the server can apply XML that it is much more difficult to execute if to use the usual text; but it does not concern searches. Therefore if you do not deal with a script which supposes only XML (and is also such), it is more safe to you to use the usual text almost in all situations at transfer of searches.
The conclusion
To you now, definitely, apparently, that you know how to start to work with XML in Ajax. You know, that Ajax-applications should not apply XML, and that XML is not some kind of miracle means for data transfer. To you also it should be rather comfortable send XML from Web-page on the server. That it is even more important, you know, that the server it will be valid to work and respond on your searches: you should make sure, that the server script supposes XML, and that supposes it{him} in a format which you use for transfer of the data.
You also should realize precisely, why XML - not always a good choice as a data format for searches. In subsequent clauses{articles}, you will familiarize with some cases when it helps, but in the majority searches he simply slows down and complicates job. Therefore, though I usually would suggest, that you at once started application of everything to that you have learned in clause{article}, I instead of it shall advise you to be cautious in application of the received data in given clause{article}. XML-searches find the scope in Ajax-applications, but this area not such extensive as you can represent yourselves.
In following clause{article} of this series, you learn{find out}, how servers can respond with help XML and as your Web-applications can work with these answers. Fortunately, is much more the reasons that the server sent XML back in the Web-application, than on the contrary, therefore you receive even more advantage from technical details of clause{article}; and now, make sure, that you understand, why XML is not always expedient - at least, for departure of searches. Probably, you even want to try to execute some Web-applications with help XML as a data format for searches, and then to convert back in the usual text, and to see, that it is faster and easier for you. I still shall meet you in a network before publication of following clause{article}.
For what it is possible to obtain ban from search engines!
For what it is possible to obtain ban from search engines!
Rather often at creation of a site of us guards an idea on a bath of the future resource. Clause{article} is not great, but gives enough information to reflection.

|