~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to examples/E4X/e4x_example.js

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is Rhino code, released
 
14
 * May 6, 1999.
 
15
 *
 
16
 * The Initial Developer of the Original Code is Netscape
 
17
 * Communications Corporation.  Portions created by Netscape are
 
18
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 
19
 * Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * John Schneider
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the
 
25
 * terms of the GNU Public License (the "GPL"), in which case the
 
26
 * provisions of the GPL are applicable instead of those above.
 
27
 * If you wish to allow use of your version of this file only
 
28
 * under the terms of the GPL and not to allow others to use your
 
29
 * version of this file under the NPL, indicate your decision by
 
30
 * deleting the provisions above and replace them with the notice
 
31
 * and other provisions required by the GPL.  If you do not delete
 
32
 * the provisions above, a recipient may use your version of this
 
33
 * file under either the NPL or the GPL.
 
34
 */
 
35
 
 
36
print("----------------------------------------");
 
37
 
 
38
// Use the XML constructor to parse an string into an XML object
 
39
var John = "<employee><name>John</name><age>25</age></employee>";
 
40
var Sue ="<employee><name>Sue</name><age>32</age></employee>";
 
41
var tagName = "employees";
 
42
var employees = new XML("<" + tagName +">" + John + Sue + "</" + tagName +">");
 
43
print("The employees XML object constructed from a string is:\n" + employees);
 
44
 
 
45
print("----------------------------------------");
 
46
 
 
47
// Use an XML literal to create an XML object
 
48
var order = <order>
 
49
   <customer>
 
50
      <firstname>John</firstname>
 
51
      <lastname>Doe</lastname>
 
52
   </customer>
 
53
   <item>
 
54
      <description>Big Screen Television</description>
 
55
      <price>1299.99</price>
 
56
      <quantity>1</quantity>
 
57
   </item>
 
58
</order>
 
59
 
 
60
// Construct the full customer name
 
61
var name = order.customer.firstname + " " + order.customer.lastname;
 
62
 
 
63
// Calculate the total price
 
64
var total = order.item.price * order.item.quantity;
 
65
 
 
66
print("The order XML object constructed using a literal is:\n" + order);
 
67
print("The total price of " + name + "'s order is " + total);
 
68
 
 
69
print("----------------------------------------");
 
70
 
 
71
// construct a new XML object using expando and super-expando properties
 
72
var order = <order/>;
 
73
order.customer.name = "Fred Jones";
 
74
order.customer.address.street = "123 Long Lang";
 
75
order.customer.address.city = "Underwood";
 
76
order.customer.address.state = "CA";
 
77
order.item[0] = "";
 
78
order.item[0].description = "Small Rodents";
 
79
order.item[0].quantity = 10;
 
80
order.item[0].price = 6.95;
 
81
 
 
82
print("The order custructed using expandos and super-expandos is:\n" + order);
 
83
 
 
84
// append a new item to the order
 
85
order.item += <item><description>Catapult</description><price>139.95</price></item>;
 
86
 
 
87
print("----------------------------------------");
 
88
 
 
89
print("The order after appending a new item is:\n" + order);
 
90
 
 
91
print("----------------------------------------");
 
92
 
 
93
// dynamically construct an XML element using embedded expressions
 
94
var tagname = "name";
 
95
var attributename = "id";
 
96
var attributevalue = 5;
 
97
var content = "Fred";
 
98
 
 
99
var x = <{tagname} {attributename}={attributevalue}>{content}</{tagname}>;
 
100
 
 
101
print("The dynamically computed element value is:\n" + x.toXMLString());
 
102
 
 
103
print("----------------------------------------");
 
104
 
 
105
// Create a SOAP message
 
106
var message = <soap:Envelope
 
107
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 
108
      soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 
109
   <soap:Body>
 
110
      <m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
 
111
         <symbol>DIS</symbol>
 
112
      </m:GetLastTradePrice>
 
113
   </soap:Body>
 
114
</soap:Envelope>
 
115
 
 
116
// declare the SOAP and stocks namespaces
 
117
var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
 
118
var stock = new Namespace ("http://mycompany.com/stocks");
 
119
 
 
120
// extract the soap encoding style and body from the soap message
 
121
var encodingStyle = message.@soap::encodingStyle;
 
122
 
 
123
print("The encoding style of the soap message is specified by:\n" + encodingStyle);
 
124
 
 
125
// change the stock symbol
 
126
message.soap::Body.stock::GetLastTradePrice.symbol = "MYCO";
 
127
 
 
128
var body = message.soap::Body;
 
129
 
 
130
print("The body of the soap message is:\n" + body);
 
131
 
 
132
print("----------------------------------------");
 
133
 
 
134
// create an manipulate an XML object using the default xml namespace
 
135
 
 
136
default xml namespace = "http://default.namespace.com";
 
137
var x = <x/>;
 
138
x.a = "one";
 
139
x.b = "two";
 
140
x.c = <c xmlns="http://some.other.namespace.com">three</c>;
 
141
 
 
142
print("XML object constructed using the default xml namespace:\n" + x);
 
143
 
 
144
default xml namespace="";
 
145
 
 
146
print("----------------------------------------");
 
147
 
 
148
var order = <order id = "123456" timestamp="Mon Mar 10 2003 16:03:25 GMT-0800 (PST)">
 
149
   <customer>
 
150
      <firstname>John</firstname>
 
151
      <lastname>Doe</lastname>
 
152
   </customer>
 
153
   <item id="3456">
 
154
      <description>Big Screen Television</description>
 
155
      <price>1299.99</price>
 
156
      <quantity>1</quantity>
 
157
   </item>
 
158
   <item id = "56789">
 
159
      <description>DVD Player</description>
 
160
      <price>399.99</price>
 
161
      <quantity>1</quantity>
 
162
   </item>
 
163
</order>;
 
164
 
 
165
 
 
166
// get the customer element from the orderprint("The customer is:\n" + order.customer);
 
167
 
 
168
// get the id attribute from the order
 
169
print("The order id is:" + order.@id);
 
170
 
 
171
// get all the child elements from the order element
 
172
print("The children of the order are:\n" + order.*); 
 
173
 
 
174
// get the list of all item descriptions
 
175
print("The order descriptions are:\n" + order.item.description); 
 
176
 
 
177
 
 
178
// get second item by numeric index
 
179
print("The second item is:\n" + order.item[1]);
 
180
 
 
181
// get the list of all child elements in all item elements
 
182
print("The children of the items are:\n" + order.item.*);
 
183
 
 
184
// get the second child element from the order by index
 
185
print("The second child of the order is:\n" + order.*[1]);
 
186
 
 
187
// calculate the total price of the order
 
188
var totalprice = 0;
 
189
for each (i in order.item) {
 
190
        totalprice += i.price * i.quantity;
 
191
}
 
192
print("The total price of the order is: " + totalprice);
 
193
 
 
194
print("----------------------------------------");
 
195
 
 
196
var e = <employees>
 
197
   <employee id="1"><name>Joe</name><age>20</age></employee>
 
198
   <employee id="2"><name>Sue</name><age>30</age></employee>
 
199
</employees>;
 
200
 
 
201
// get all the names in e
 
202
print("All the employee names are:\n" + e..name);
 
203
 
 
204
// employees with name Joe
 
205
print("The employee named Joe is:\n" + e.employee.(name == "Joe"));
 
206
 
 
207
// employees with id's 1 & 2
 
208
print("Employees with ids 1 & 2:\n" + e.employee.(@id == 1 || @id == 2)); 
 
209
 
 
210
// name of employee with id 1
 
211
print("Name of the the employee with ID=1: " + e.employee.(@id == 1).name);
 
212
 
 
213
print("----------------------------------------");
 
214
 
 
215
 
 
216
 
 
217
 
 
218
 
 
219
 
 
220
 
 
221