public static String getElementContent ( final Element el , final boolean trim ) throws SAXException { StringBuilder sb = new StringBuilder ( ) ; NodeList children = el . getChildNodes ( ) ; for ( int i = 0 ; i < children . getLength ( ) ; i ++ ) { Node curnode = children . item ( i ) ; if ( curnode . getNodeType ( ) == Node . TEXT_NODE ) { sb . append ( curnode . getNodeValue ( ) ) ; } else if ( curnode . getNodeType ( ) == Node . CDATA_SECTION_NODE ) { sb . append ( curnode . getNodeValue ( ) ) ; } else if ( curnode . getNodeType ( ) == Node . COMMENT_NODE ) { // Ignore } else { throw new SAXException ( "Unexpected child node " + curnode . getLocalName ( ) + " for " + el . getLocalName ( ) ) ; } } if ( ! trim ) { return sb . toString ( ) ; } return sb . toString ( ) . trim ( ) ; }
Return the content for the current element . All leading and trailing whitespace and embedded comments will be removed .