Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 58 59 60 61 62 63 < 64 > 65 66 67 68 69 70 .. 166 >> Next

236: ByteArraylnputStream bais = new
ByteArrayInputStream(response.getBytes());
237: doc = db.parse(bais);
238: if (doc != null)
239: removeBlankNodes(doc.getDocumentElement());
240:
241: productNodes = 2
doc.getElementsByTagName(ELEMENT_PRODUCT_NAME);
242: if (productNodes != null)
243: {
244: int len = productNodes.getLength();
245: productNames = new String[len];
246: for (int i=0; i < len; i++)
247: {
248: Node n = productNodes.item(i);
249: Node t = n.getFirstChild();
250: if (t.getNodeType() == Node.TEXT_NODE)
251: productNames[i] = t.getNodeValue();
252: }
253: }
Listing 22.1 (continued)
J2ME Performance and Pitfalls 169
254:
255: if (productNames != null && productNames.length > 0)
256: {
257: // populate the list
258: results.setListData(productNames);
259: }
// - removed else error condition handling for brevity
267: }
// - removed utility method isBlank()
// - removed utility method removeBlankNodes()
// - removed utility method findNodeWithContent()
// - removed main() which merely Instantiates SwinginAmazonSearch
333: }
Listing 22.1 (continued)
The method doAmazonSearch() (lines 209 to 267) has four key functions:
Format a URL.
Send an HTTP GET request to xml.amazon.com.
Parse the resulting XML to extract the product names.
Populate the JList with the product names.
Both formatting the URL and "getting" it are performed in conjunction with the AmazonHttpGet class discussed later. It is important to note the modularity of the formatting operation for the URL displayed in lines 221 to 225. The formatting of a URL is broken into separate functions to assemble the URL in any order or length you want. Such modularity and the building of "generic code" will have to be sacrificed in our J2ME implementation for speed. At line 228, the httpGet() method is invoked which returns a String containing an XML document. Here is a portion of a sample return document:
<Details url="http://www.amazon.com/exec/obidos/redirect?tag=webservices-20%2 6creative=D3AG4L7PI53LPH%2 6camp=2025%2 6link_code=xm2%2 6path=ASIN/047 1237515">
<Asin>0471237515</Asin>
<ProductName>More Java Pitfalls: 50 New Time-Saving Solutions and 2 Workarounds</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>Michael C. Daconta</Author>
<Author>Kevin T. Smith</Author>
<Author>Donald Avondolio </Author>
<Author>W. Clay Richardson</Author>
</Authors>
<ReleaseDate>03 February, 2003</ReleaseDate>
<Manufacturer>John Wiley &amp; Sons</Manufacturer> <ListPrice>$40.00</ListPrice>
<OurPrice>$40.00</OurPrice>
</Details>
170 Item 22
The XML response is then fed into the standard JDK XML Parser, and a Document Object Model (DOM) is constructed (the org.w3c.Document class) at line 237. All the elements with a tag of "ProductName" are retrieved (line 241) and their text content extracted. Since the XML document is guaranteed to be only 10 products at a time, this Swing application can comfortably construct a DOM even though it requires more memory than a SAX Parser or Pull Parser approach; however, this approach proves to use too much memory in our J2ME port. Several utility methods were created to search and manipulate the DOM, like removeBlankNodes() and findNodeWithContent(), but were deleted since they do not pertain to J2ME pitfalls:
335 class DetailsDialog extends JDialog
336 {
337 JTextArea textArea = new JTextArea(5,60);
338 public DetailsDialog(Frame f, Node detailsNode, String 2
productName)
339 {
340 super(f, "Details for " + productName, false);
341
342 getContentPane().setLayout(new BorderLayout(2,2));
343 JScrollPane scroller = new JScrollPane(textArea);
344 getContentPane().add("Center", scroller);
345
346 // initialize the text Area
347 textArea.setEditable(false);
348 textArea.setBackground(Color.lightGray);
349
350 NodeList children = detailsNode.getChildNodes();
351 int len = children.getLength();
352 for (int i=0; i < len; i++)
353 {
354 // display element children with a text node
355 Node child = children.item(i);
356 if (child.getNodeType() == Node.ELEMENT_NODE)
357 {
358 Node txt = child.getFirstChild();
359 if (txt != null && txt.getNodeType() == Node.TEXT_ E) D O N
360 {
361 String label = child.getNodeName();
362 String value = txt.getNodeValue();
363
364 if (value.length() > 0)
365 textArea.append("" + label + ": " + value +
"\n );
366 }
367 }
368 }
369
// removed setting window size and location
372 this.setVisible(true);
373 }
374 }
Listing 22.1 (continued)
J2ME Performance and Pitfalls 171
375:
// - removed StatusPanel Class for brevity 439:
Listing 22.1 (continued)
The DetailsDialog presents a frame with a single JTextArea where each tag name becomes the label (line 361) and each node value of the XML element is presented as the labels value separated by a colon (line 365). This approach relies on detailed knowledge of the XML format.
Previous << 1 .. 58 59 60 61 62 63 < 64 > 65 66 67 68 69 70 .. 166 >> Next