public static List < Element > getElements ( final Node nd ) throws SAXException { final List < Element > al = new ArrayList <> ( ) ; NodeList children = nd . getChildNodes ( ) ; for ( int i = 0 ; i < children . getLength ( ) ; i ++ ) { Node curnode = children . item ( i ) ; if ( curnode . getNodeType ( ) == Node . TEXT_NODE ) { String val = curnode . getNodeValue ( ) ; if ( val != null ) { for ( int vi = 0 ; vi < val . length ( ) ; vi ++ ) { if ( ! Character . isWhitespace ( val . charAt ( vi ) ) ) { throw new SAXException ( "Non-whitespace text in element body for " + nd . getLocalName ( ) + "\n text=" + val ) ; } } } } else if ( curnode . getNodeType ( ) == Node . COMMENT_NODE ) { // Ignore } else if ( curnode . getNodeType ( ) == Node . ELEMENT_NODE ) { al . add ( ( Element ) curnode ) ; } else { throw new SAXException ( "Unexpected child node " + curnode . getLocalName ( ) + " for " + nd . getLocalName ( ) ) ; } } return al ; }
All the children must be elements or white space text nodes .