Ajax|web|web Service | Script This article briefly introduces the ECMAScript (ECMAScript for xml,e4x) that can be used for XML, which is a simple extension of JavaScript, which makes it very easy to write XML scripts. In this article, the author demonstrates a WEB programming model called Asynchronous JavaScript and XML (asynchronous JavaScript and Xml,ajax) and shows you how some of the new XML extensions for JavaScript make it easier to implement.
ECMAScript for XML
You may not have encountered this term ECMAScript before. It is actually the official name of JavaScript. The European Computer Manufacturers Association (European Computer Manufacturers ASSOCIATION,ECMA) is a standardized organization for the development of JAVASCRIPT standards (while C # and CLR standards are also developed by the Association). The ECMAScript standard can be obtained free of charge from the ECMA Web site.
E4X is an extension of JavaScript, which actually adds direct support to XML in JavaScript. It is also a ECMA standard (see Resources--ecma-357). So what is its direct support for XML? Why does it have a high value? If you're a JavaScript programmer, you might have used something like Netscape liveconnect or Rhino (a free Jav that runs under Java™) Ascript Library) To use these Java libraries in your JavaScript. This means that you can already create, manipulate, and use XML with the help of an XML library. Similarly, if you are using Microsoft®internet Explorer, you have obtained XML support through the Microsoft MSXML2 library. That way, if you've already used those library files, be prepared to make important changes--e4x easier and easier.
Before we look at the sample example, we need to be aware of the experiment: there are two E4X implementations available at this time. Both of these options are available from Mozilla. One is the C JavaScript engine used by browsers (which is named SpiderMonkey by code) and is available in the latest Mozilla version-we use Mozilla 1.8a6. E4X is also available in Rhino. Rhino is a Java-built JavaScript interpreter and compiler, and we'll show you how to use it and run it inside of Axis. Both can be obtained from Mozilla.
In these sample instances, we start with the e4x on the command line using Rhino, and then we turn to the use of e4x within the browser using Mozilla, while demonstrating the AJAX model. In the second article, we'll show you how to use the e4x inside the server by embedding Rhino in the Apache Axis Web service engine. But before we turn to WEB services, we'll show you the basics of XML programming in e4x.
A simple example
Let's start with a simple example. We first analyze and manipulate the XML that represents the relevant information about the author. The XML we want is as follows:
Listing 1. Author XML document
<people>
<person gender= "Male" >
<name>Ant</name>
<eyes>Blue</eyes>
</person>
<person gender= "Male" >
<name>Paul</name>
<eyes>Grey</eyes>
</person>
</people>
If we get the XML document as a string, we can "parse" the string by using the following simple actions:
var x = new XML (peoplexmlstring);
Alternatively, we can simply "embed" the XML into the code:
var x =
<people>
<person gender= "Male" >
<name>Ant</name>
<eyes>Blue</eyes>
</person>
<person gender= "Male" >
<name>Paul</name>
<eyes>Grey</eyes>
</person>
</people>;
Yes-that's right--xml became a direct part of the language .
use e4x via Rhino
If you're doing exactly the same thing as we do, you can now try the operation . the latest version of Rhino 1.6R1 and the XMLBeans Library from Apache support e4x. using them is very simple, just get these packages, unzip them, and add Js.jar and Xbean.jar to your classpath, you can start the JavaScript handler .
JAVA-CP Js.jar;xbean.jar
Org.mozilla.javascript.tools.shell.Main
You can now try the following examples . you can cut and copy them from there, or include them in the Examples1.js file, which is in a zip file (ws-ajax1code.zip), and you can download the file by clicking the code icon at the top or bottom of this article .
You can view any part of the XML simply by using the properties of JavaScript . For example:
Print (x.person[0].name);
Ant
Print (X.person[1].hair);
Spiky
Have you noticed that, so far, we haven't used XML APIs such as DOM or SAX. XML just becomes a local type that JavaScript can understand .
Use the following code to print the height of two people:
For each (Var h in X..) Height) {print (h)};
176
178
syntax is very useful . it returns all of the child elements of any depth that can match the tag name immediately following it . so x.. Height returns the value of the height marker .
Here's another useful syntax:
Print (X.person. ( name== "Ant"). Hair);
Shaggy
This makes it easy to view operations in the XML tree .
A more complex sample instance
Suppose you want to change the height value from the metric unit to the Anglo-American system (that is, the feet and inch units that the British often say).
The next step is centimeter to inch conversion ...
function Metrictoimperial (CMS) {
var totalinches = Math.Round (cms/2.54);
var inch = totalinches%12;
var ft = (totalinches-inch)/12;
var response =
Response.feet = ft;
Response.inches = inch + 2; We sounded a bit short
Response. @measure = "Imperial";
return response;
}
The first notable line is:
var response =
This line allows you to "embed" XML into JavaScript . There are two advantages to adopting this syntax: first, it does make XML easy to use . Second, the meaning of these codes is very clear .
You can now add more child elements to the element:
Response.feet = ft;
This creates a .
You can also manipulate its properties in this way:
Response. @measure = "Imperial";
Now we use this feature to update the XML:
- Ajax: A new way to build Web apps
- Discussion on the error handling mechanism of AJAX (2)
- Discussion on the error handling mechanism of AJAX (1)
- First experience. NET Ajax Brushless New technology
- A brief analysis of Ajax development Technology in Rails system (4)