| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
print("----------------------------------------"); |
| 8 |
|
| 9 |
|
| 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 |
|
| 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 |
|
| 32 |
var name = order.customer.firstname + " " + order.customer.lastname; |
| 33 |
|
| 34 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 88 |
var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/"); |
| 89 |
var stock = new Namespace ("http://mycompany.com/stocks"); |
| 90 |
|
| 91 |
|
| 92 |
var encodingStyle = message.@soap::encodingStyle; |
| 93 |
|
| 94 |
print("The encoding style of the soap message is specified by:\n" + encodingStyle); |
| 95 |
|
| 96 |
|
| 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 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
print("The order id is:" + order.@id); |
| 141 |
|
| 142 |
|
| 143 |
print("The children of the order are:\n" + order.*); |
| 144 |
|
| 145 |
|
| 146 |
print("The order descriptions are:\n" + order.item.description); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
print("The second item is:\n" + order.item[1]); |
| 151 |
|
| 152 |
|
| 153 |
print("The children of the items are:\n" + order.item.*); |
| 154 |
|
| 155 |
|
| 156 |
print("The second child of the order is:\n" + order.*[1]); |
| 157 |
|
| 158 |
|
| 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 |
|
| 173 |
print("All the employee names are:\n" + e..name); |
| 174 |
|
| 175 |
|
| 176 |
print("The employee named Joe is:\n" + e.employee.(name == "Joe")); |
| 177 |
|
| 178 |
|
| 179 |
print("Employees with ids 1 & 2:\n" + e.employee.(@id == 1 || @id == 2)); |
| 180 |
|
| 181 |
|
| 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 |
|