StringWriter writerUsingWriteAsEncodedUnicode = new StringWriter();
StringWriter writerUsingToString = new StringWriter();
Location startLocation = null;
try
{
XMLEvent event = xmlEventReader.peek();
if ( ! event.isStartElement() )
throw new IllegalArgumentException( "Next event in reader must be start element." );
startLocation = event.getLocation();
// Track start and end elements so know when done.
// Use name list as FILO, push on name of start element and pop off matching name of end element.
List<QName> nameList = new ArrayList<QName>();
while ( xmlEventReader.hasNext() )
{
event = xmlEventReader.nextEvent();
if ( event.isStartElement() )
{
nameList.add( event.asStartElement().getName() );
}
else if ( event.isEndElement() )
{
QName endElemName = event.asEndElement().getName();
QName lastName = nameList.get( nameList.size() - 1 );
if ( lastName.equals( endElemName ) )
nameList.remove( nameList.size() - 1 );
else
{
// Parser should have had FATAL error for this.
String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "].";
log.error( "consumeElementAndConvertToXmlString(): " + msg );
throw new ThreddsXmlParserException( "FATAL? " + msg );
}
}
event.writeAsEncodedUnicode( writerUsingWriteAsEncodedUnicode );
writerUsingToString.write( event.toString());
if ( nameList.isEmpty() )
break;
}
}
catch ( XMLStreamException e )