1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/
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.
13
* The Original Code is Rhino code, released
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
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.
36
print("----------------------------------------");
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);
45
print("----------------------------------------");
47
// Use an XML literal to create an XML object
50
<firstname>John</firstname>
51
<lastname>Doe</lastname>
54
<description>Big Screen Television</description>
55
<price>1299.99</price>
56
<quantity>1</quantity>
60
// Construct the full customer name
61
var name = order.customer.firstname + " " + order.customer.lastname;
63
// Calculate the total price
64
var total = order.item.price * order.item.quantity;
66
print("The order XML object constructed using a literal is:\n" + order);
67
print("The total price of " + name + "'s order is " + total);
69
print("----------------------------------------");
71
// construct a new XML object using expando and super-expando properties
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";
78
order.item[0].description = "Small Rodents";
79
order.item[0].quantity = 10;
80
order.item[0].price = 6.95;
82
print("The order custructed using expandos and super-expandos is:\n" + order);
84
// append a new item to the order
85
order.item += <item><description>Catapult</description><price>139.95</price></item>;
87
print("----------------------------------------");
89
print("The order after appending a new item is:\n" + order);
91
print("----------------------------------------");
93
// dynamically construct an XML element using embedded expressions
95
var attributename = "id";
96
var attributevalue = 5;
99
var x = <{tagname} {attributename}={attributevalue}>{content}</{tagname}>;
101
print("The dynamically computed element value is:\n" + x.toXMLString());
103
print("----------------------------------------");
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/">
110
<m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
112
</m:GetLastTradePrice>
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");
120
// extract the soap encoding style and body from the soap message
121
var encodingStyle = message.@soap::encodingStyle;
123
print("The encoding style of the soap message is specified by:\n" + encodingStyle);
125
// change the stock symbol
126
message.soap::Body.stock::GetLastTradePrice.symbol = "MYCO";
128
var body = message.soap::Body;
130
print("The body of the soap message is:\n" + body);
132
print("----------------------------------------");
134
// create an manipulate an XML object using the default xml namespace
136
default xml namespace = "http://default.namespace.com";
140
x.c = <c xmlns="http://some.other.namespace.com">three</c>;
142
print("XML object constructed using the default xml namespace:\n" + x);
144
default xml namespace="";
146
print("----------------------------------------");
148
var order = <order id = "123456" timestamp="Mon Mar 10 2003 16:03:25 GMT-0800 (PST)">
150
<firstname>John</firstname>
151
<lastname>Doe</lastname>
154
<description>Big Screen Television</description>
155
<price>1299.99</price>
156
<quantity>1</quantity>
159
<description>DVD Player</description>
160
<price>399.99</price>
161
<quantity>1</quantity>
166
// get the customer element from the orderprint("The customer is:\n" + order.customer);
168
// get the id attribute from the order
169
print("The order id is:" + order.@id);
171
// get all the child elements from the order element
172
print("The children of the order are:\n" + order.*);
174
// get the list of all item descriptions
175
print("The order descriptions are:\n" + order.item.description);
178
// get second item by numeric index
179
print("The second item is:\n" + order.item[1]);
181
// get the list of all child elements in all item elements
182
print("The children of the items are:\n" + order.item.*);
184
// get the second child element from the order by index
185
print("The second child of the order is:\n" + order.*[1]);
187
// calculate the total price of the order
189
for each (i in order.item) {
190
totalprice += i.price * i.quantity;
192
print("The total price of the order is: " + totalprice);
194
print("----------------------------------------");
197
<employee id="1"><name>Joe</name><age>20</age></employee>
198
<employee id="2"><name>Sue</name><age>30</age></employee>
201
// get all the names in e
202
print("All the employee names are:\n" + e..name);
204
// employees with name Joe
205
print("The employee named Joe is:\n" + e.employee.(name == "Joe"));
207
// employees with id's 1 & 2
208
print("Employees with ids 1 & 2:\n" + e.employee.(@id == 1 || @id == 2));
210
// name of employee with id 1
211
print("Name of the the employee with ID=1: " + e.employee.(@id == 1).name);
213
print("----------------------------------------");