private String getNext ( ) { StringBuilder result = new StringBuilder ( ) ; try { XMLUtils . XMLTag tag ; do { // String text = XMLUtils . readUntilTag ( inputReader ) ; // there may or may not be text before the next tag, but we discard it // System.out.println("outside text: " + text ); tag = XMLUtils . readAndParseTag ( inputReader ) ; // System.out.println("outside tag: " + tag); if ( tag == null ) { return null ; // couldn't find any more tags, so no more elements } } while ( ! tagNamePattern . matcher ( tag . name ) . matches ( ) || tag . isEndTag || tag . isSingleTag ) ; if ( keepDelimitingTags ) { result . append ( tag . toString ( ) ) ; } int depth = 1 ; while ( true ) { String text = XMLUtils . readUntilTag ( inputReader ) ; if ( text != null ) { // if the text isn't null, we append it // System.out.println("inside text: " + text ); result . append ( text ) ; } String tagString = XMLUtils . readTag ( inputReader ) ; tag = XMLUtils . parseTag ( tagString ) ; if ( tag == null ) { return null ; // unexpected end of this element, so no more elements } if ( tagNamePattern . matcher ( tag . name ) . matches ( ) && tag . isEndTag ) { if ( ( countDepth && depth == 1 ) || ! countDepth ) { if ( keepDelimitingTags ) { result . append ( tagString ) ; } // this is our end tag so we stop break ; } else { -- depth ; if ( keepInternalTags ) { result . append ( tagString ) ; } } } else if ( tagNamePattern . matcher ( tag . name ) . matches ( ) && ! tag . isEndTag && ! tag . isSingleTag && countDepth ) { ++ depth ; if ( keepInternalTags ) { result . append ( tagString ) ; } } else { // not our end tag, so we optionally append it and keep going if ( keepInternalTags ) { result . append ( tagString ) ; } } } } catch ( Exception e ) { e . printStackTrace ( ) ; } return result . toString ( ) ; }
returns null if there is no next object