[jdom-interest] Deprecating hasChildren()

Elliotte Rusty Harold elharo at metalab.unc.edu
Sat May 11 09:49:42 PDT 2002


At 3:50 AM -0400 5/11/02, cvs at jools.org wrote:
>Deprecated elt.hasChildren() because it's not a performance optimization
>anymore.  This helps simplify the most complicated class around.
>

I have one use-case for that method that doesn't involve 
optimization. Consider this street element:

<street>135 Airline Highway <apartment>2B</apartment></street>

getText() returns "135 Airline Highway ". The content in the child 
apartment element is completely lost. Before you can reliably use any 
of the getText()/getTextTrim()/ getTextNormalize() methods you need 
to be very sure that the element does not have any child elements. 
One way to do this is to test with hasChildren() before invoking the 
text getter. For example,

if (!element.hasChildren()) {
    String result = element.getText();
    // work with result Š
}
else {
   // do something more complex Š
}
-- 

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
|          The XML Bible, 2nd Edition (Hungry Minds, 2001)           |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      |
|  Read Cafe con Leche for XML News: http://www.cafeconleche.org/    |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list