Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 4
getFirstChildElement() method will skip any intermediate nodes that are not of type ELEMENT_NODE.
text nodes from the document.
While both solutions will work, the second approach might remove some white space not considered ignorable.
A run of GoodDomLookup.java produces:
C:\classes\com\javaworld\jpitfalls\article4>java com.javaworld.jpitfalls.article4.GoodDomLookup myaddresses.xml Method #1: Skip Ignorable White space... # of "ADDRESS" elements: 2 This node name is: ADDRESS This node name is: NAME Method #2: Normalize document... # of "ADDRESS" elements: 2 This node name is: ADDRESS This node name is: NAME
The results show that both approaches succeed in finding the correct NAME element.
Each pitfall in this article was caused by our hypothetical programmer developing an incorrect conceptual model of how an API operates. I do not believe there is any blame to place here; the pitfall is neither the fault of the programmer nor of the API developer. The reality is that not all APIs can be simple. A Swing GUI has a single-threaded, ordered event model, and the DOM includes ignorable white space in its tree of nodes. The solutions I outlined to the pitfalls merely took those implementation realities into account. Once your conceptual model matches the complexity of the implementation, you can easily avoid these Java traps.
Read more about Core Java in JavaWorld's Core Java section.