Debian Codegraph @

rhino/examples/E4X/e4x_example.js

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 print("----------------------------------------");
8
9 // Use the XML constructor to parse an string into an XML object
10 var John = "<employee><name>John</name><age>25</age></employee>";
11 var Sue ="<employee><name>Sue</name><age>32</age></employee>";
12 var tagName = "employees";
13 var employees = new XML("<" + tagName +">" + John + Sue + "</" + tagName +">");
14 print("The employees XML object constructed from a string is:\n" + employees);
15
16 print("----------------------------------------");
17
18 // Use an XML literal to create an XML object
19 var order = <order>
20 <customer>
21 <firstname>John</firstname>
22 <lastname>Doe</lastname>
23 </customer>
24 <item>
25 <description>Big Screen Television</description>
26 <price>1299.99</price>
27 <quantity>1</quantity>
28 </item>
29 </order>
30
31 // Construct the full customer name
32 var name = order.customer.firstname + " " + order.customer.lastname;
33
34 // Calculate the total price
35 var total = order.item.price * order.item.quantity;
36
37 print("The order XML object constructed using a literal is:\n" + order);
38 print("The total price of " + name + "'s order is " + total);
39
40 print("----------------------------------------");
41
42 // construct a new XML object using expando and super-expando properties
43 var order = <order/>;
44 order.customer.name = "Fred Jones";
45 order.customer.address.street = "123 Long Lang";
46 order.customer.address.city = "Underwood";
47 order.customer.address.state = "CA";
48 order.item[0] = "";
49 order.item[0].description = "Small Rodents";
50 order.item[0].quantity = 10;
51 order.item[0].price = 6.95;
52
53 print("The order custructed using expandos and super-expandos is:\n" + order);
54
55 // append a new item to the order
56 order.item += <item><description>Catapult</description><price>139.95</price></item>;
57
58 print("----------------------------------------");
59
60 print("The order after appending a new item is:\n" + order);
61
62 print("----------------------------------------");
63
64 // dynamically construct an XML element using embedded expressions
65 var tagname = "name";
66 var attributename = "id";
67 var attributevalue = 5;
68 var content = "Fred";
69
70 var x = <{tagname} {attributename}={attributevalue}>{content}</{tagname}>;
71
72 print("The dynamically computed element value is:\n" + x.toXMLString());
73
74 print("----------------------------------------");
75
76 // Create a SOAP message
77 var message = <soap:Envelope
78 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
79 soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
80 <soap:Body>
81 <m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
82 <symbol>DIS</symbol>
83 </m:GetLastTradePrice>
84 </soap:Body>
85 </soap:Envelope>
86
87 // declare the SOAP and stocks namespaces
88 var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
89 var stock = new Namespace ("http://mycompany.com/stocks");
90
91 // extract the soap encoding style and body from the soap message
92 var encodingStyle = message.@soap::encodingStyle;
93
94 print("The encoding style of the soap message is specified by:\n" + encodingStyle);
95
96 // change the stock symbol
97 message.soap::Body.stock::GetLastTradePrice.symbol = "MYCO";
98
99 var body = message.soap::Body;
100
101 print("The body of the soap message is:\n" + body);
102
103 print("----------------------------------------");
104
105 // create an manipulate an XML object using the default xml namespace
106
107 default xml namespace = "http://default.namespace.com";
108 var x = <x/>;
109 x.a = "one";
110 x.b = "two";
111 x.c = <c xmlns="http://some.other.namespace.com">three</c>;
112
113 print("XML object constructed using the default xml namespace:\n" + x);
114
115 default xml namespace="";
116
117 print("----------------------------------------");
118
119 var order = <order id = "123456" timestamp="Mon Mar 10 2003 16:03:25 GMT-0800 (PST)">
120 <customer>
121 <firstname>John</firstname>
122 <lastname>Doe</lastname>
123 </customer>
124 <item id="3456">
125 <description>Big Screen Television</description>
126 <price>1299.99</price>
127 <quantity>1</quantity>
128 </item>
129 <item id = "56789">
130 <description>DVD Player</description>
131 <price>399.99</price>
132 <quantity>1</quantity>
133 </item>
134 </order>;
135
136
137 // get the customer element from the orderprint("The customer is:\n" + order.customer);
138
139 // get the id attribute from the order
140 print("The order id is:" + order.@id);
141
142 // get all the child elements from the order element
143 print("The children of the order are:\n" + order.*);
144
145 // get the list of all item descriptions
146 print("The order descriptions are:\n" + order.item.description);
147
148
149 // get second item by numeric index
150 print("The second item is:\n" + order.item[1]);
151
152 // get the list of all child elements in all item elements
153 print("The children of the items are:\n" + order.item.*);
154
155 // get the second child element from the order by index
156 print("The second child of the order is:\n" + order.*[1]);
157
158 // calculate the total price of the order
159 var totalprice = 0;
160 for each (i in order.item) {
161 totalprice += i.price * i.quantity;
162 }
163 print("The total price of the order is: " + totalprice);
164
165 print("----------------------------------------");
166
167 var e = <employees>
168 <employee id="1"><name>Joe</name><age>20</age></employee>
169 <employee id="2"><name>Sue</name><age>30</age></employee>
170 </employees>;
171
172 // get all the names in e
173 print("All the employee names are:\n" + e..name);
174
175 // employees with name Joe
176 print("The employee named Joe is:\n" + e.employee.(name == "Joe"));
177
178 // employees with id's 1 & 2
179 print("Employees with ids 1 & 2:\n" + e.employee.(@id == 1 || @id == 2));
180
181 // name of employee with id 1
182 print("Name of the the employee with ID=1: " + e.employee.(@id == 1).name);
183
184 print("----------------------------------------");
185
186
187
188
189
190
191