Code example for XmlSerializer

Methods: endDocument, endTag, flush, setOutput, startDocument, startTag, text

0
    public void marshall( Object object, Writer writer )
        throws IOException, MarshallingException 
    { 
        try 
        { 
            xmlSerializer.setOutput( writer );
            xmlSerializer.startDocument( "UTF-8", null );
            serializeObject( object );
            xmlSerializer.endDocument();
            xmlSerializer.flush();
        } 
        catch ( IOException exception )
        { 
            throw exception;
        } 
        catch ( MarshallingException exception )
        { 
            throw exception;
        } 
        catch ( Exception cause )
        { 
            throw new MarshallingException( cause );
        } 
    } 
 
    private void serializeObject( Object object )
        throws IllegalArgumentException, IllegalStateException, IOException, IllegalAccessException, 
        MarshallingException 
    { 
        XmlRootElement annotation = object.getClass().getAnnotation( XmlRootElement.class );
        if ( annotation != null )
        { 
            serializeObject( object, annotation.namespace(), annotation.name() );
        } 
    } 
 
    private void serializeObject( Object object, String namespace, String name )
        throws IOException, IllegalArgumentException, IllegalStateException, IllegalAccessException, 
        MarshallingException 
    { 
        xmlSerializer.startTag( namespace, name );
        XmlType annotation = object.getClass().getAnnotation( XmlType.class );
        if ( annotation != null && !annotation.isEnum() )
        { 
            serializeComplexType( object, namespace );
        } 
        else 
        { 
        	if (object instanceof Date) {
                 Date date = (Date) object;
                 xmlSerializer.text( dateFormat.format(date) );
        	} else { 
                 xmlSerializer.text( object.toString() );
         	} 
        } 
        xmlSerializer.endTag( namespace, name );
    } 
 
    private void serializeComplexType( Object object, String namespace )
        throws MarshallingException, IOException, IllegalAccessException 
    {