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 .. 24 25 26 27 28 29 < 30 > 31 32 33 34 35 36 .. 166 >> Next

Upon executing Listing 8.2, we get
e:\classes\org\javapitfalls\>java org.javapitfalls ... BadDomLookup 2
myaddresses.xml
# of "ADDRESS” elements: 2
This node name is: ADDRESS
This node name is: #text
The result clearly shows that the program fails to accomplish its task. Instead of an ADDRESS node, we get a text node. What happened? Unfortunately, the complexity of the DOM implementation is different from our simple conceptual model. The primary difference is that the DOM tree includes text nodes for what is called "ignorable whitespace," which is the whitespace (like a return) between tags. In our example, there is a text node between the ADDRESS and the first NAME element. The W3C XML specification states, "An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content."3 To visualize these whitespace nodes, Figure 8.1 displays all the DOM nodes in myaddresses.xml in a JTree.
There are three solutions to this problem, and our rewrite of the program demonstrates two of them. Listing 8.3, GoodDomLookup.java, fixes the problem demonstrated above in two ways.
File
13 [ADDRESS_BOOK: null]
D («ext: ??]
9 C3 (AODRESS: null]
0 IftextDDQ]
9 L3 (NAME: null]
0 (ftext Joe Jones |
Q |#text: ???]
®- Cl (STREET: null]
0 PtextDDQ]
o-dlCITY: null]
Q |#lexl: ???]
®" L3 (STATE: null]
Q |fle*l:DDn] î (ZIP: null]
Q (ftext: ??]
Q («ext: ??]
Î (AODRESS: null]
D («ext: 0]
Figure 8.1 Display of all DOM nodes in myaddresses.xml.
3 Extensible Markup Language (XML) 1.0 (Second Edition). W3C recommendation; October 6, 2000; http://www.w3.org/TR/REC-xml.
myaddresses.xml
The Wrong Way to Search a DOM 69
001 package org.javapitfalls.item8;
002
003 import javax.xml.parsers.*;
004 import java.io.*;
005 import org.w3c.dom.*;
006
007 class DomUtil
008 {
009 public static boolean isBlank(String buf)
010 {
011 if (buf == null)
012 return false;
013
014 int len = buf.length();
015 for (int i=0; i < len; i++)
016 {
017 char c = buf.charAt(i);
018 if (!Character.isWhitespace(c))
019 return false;
020 }
021
022 return true;
023 }
024
025 public static void normalizeDocument(Node n)
026 {
027 if (!n.hasChildNodes())
028 return;
029
030 NodeList nl = n.getChildNodes();
031 for (int i = 0; i < nl.getLength(); i++)
032 {
033 Node cn = nl.item(i);
034 if (cn.getNodeType() == Node.TEXT_ NODE &&
035 isBlank(cn.getNodeValue()))
036 {
037 n.removeChild(cn);
038 i--;
039 }
040 else
041 normalizeDocument(cn);
042 }
043 }
044
045 public static Element getFirstChildElement(Element elem)
046 {
047 if (!elem.hasChildNodes())
048 return null;
Listing 8.3 GoodDomLookup.java (continued)
70 Item 8
049
050 for (Node cn = elem.getFirstChild(); cn != null;
051 cn = cn.getNextSibling())
052 {
053 if (cn.getNodeType() == Node.ELEMENT_NODE)
054 return (Element) cn;
055 }
056
057 return null;
058 }
059 }
060
061 public class GoodDomLookup
062 {
063 public static void main(String args[])
064 {
065 try
066 {
// command line check omitted for brevity ...
073
074 DocumentBuilderFactory dbf =
075 DocumentBuilderFactory.newInstance();
076 DocumentBuilder db = dbf.newDocumentBuilder();
077 Document doc = db.parse(new File(args[0]));
078
079 // get first Name of first Address
080 System.out.println("Method #1: Skip Ignorable White 2
space.. .");
081 NodeList nl = doc.getElementsByTagName("ADDRESS");
082 int count = nl.getLength();
083 System.out.println("# of \"ADDRESS\" elements: " + count);
084
085 if (count > 0)
086 {
087 Node n = nl.item(0);
088 System.out.println("This node name is: " + 2
n.getNodeName()) ;
089 // get the NAME node of this ADDRESS node
090 Node nameNode =
DomUtil )n) t) n men e l (E t( n men e l dEl l i h C t Cfi r i F t e tn
091 System.out.println("This node name is: " +
092 nameNode.getNodeName());
093 }
094
095 // get first Name of first Address
096 System.out.println("Method #2: Normalize document... );
097 DomUtil.normalizeDocument(doc.getDocumentElement());
098 // Below is exact code in BadDomLookup
Listing 8.3 (continued)
The Wrong Way to Search a DOM 71
099: nl = doc.getElementsByTagName("ADDRESS");
100: count = nl.getLength();
101: System.out.println("# of \"ADDRESS\" elements: " +
count);
102:
103: if (count > 0)
104: {
105: Node n = nl.item(0);
106: System.out.println("This node name is: " +
107: n.getNodeName());
108: // get the NAME node of this ADDRESS node
109: Node nameNode = n.getFirstChild();
110: System.out.println("This node name is: " +
111: nameNode.getNodeName());
Previous << 1 .. 24 25 26 27 28 29 < 30 > 31 32 33 34 35 36 .. 166 >> Next