background image

The SOAPElement Class

<< Extracting the Price List | Ordering Coffee >>
<< Extracting the Price List | Ordering Coffee >>

The SOAPElement Class

The next lines of code drill down another level to retrieve the coffee-name and price elements
contained in it3. Then the message getValue retrieves the text (a coffee name or a price) that
the SAAJ coffee supplier added to the coffee-name and price elements when it gave content to
response
. The final line in the following code fragment adds the coffee name or price to the
Vector
object list. Note that because of the nested while loops, for each coffee element that
the code retrieves, both of its child elements (the coffee-name and price elements) are
retrieved.
Iterator it3 = child2.getChildElements();
while (it3.hasNext()) {
SOAPElement child3 = (SOAPElement)it3.next();
String value = child3.getValue();
list.addElement(value);
}
}
}
The final code fragment adds the coffee names and their prices (as a PriceListItem) to the
ArrayList priceItems
, and prints each pair on a separate line. Finally it constructs and returns
a PriceListBean.
ArrayList<PriceItemBean> items = new ArrayList<PriceItemBean>();
for (int i = 0; i < list.size(); i = i + 2) {
PriceItemBean pib = new PriceItemBean();
pib.setCoffeeName(list.elementAt(i).toString());
pib.setPricePerPound(new BigDecimal(list.elementAt(i + 1).toString()));
items.add(pib);
System.out.print(list.elementAt(i) +
"
");
System.out.println(list.elementAt(i + 1));
}
Date today = new Date();
Date endDate = DateHelper.addDays(today, 30);
GregorianCalendar todayCal = new GregorianCalendar();
todayCal.setTime(today);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(endDate);
plb = new PriceListBean();
plb.setStartDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(todayCal));
List<PriceItemBean> priceItems = new ArrayList<PriceItemBean>();
Iterator<PriceItemBean> i = items.iterator();
while (i.hasNext()) {
PriceItemBean pib = i.next();
plb.getPriceItems().add(pib);
}
plb.setEndDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(cal));
SAAJ Coffee Supplier Service
The Java EE 5 Tutorial · September 2007
1036