public static void main ( String[ ] args) throws Exception{
Insurance insurance = new Insurance ( ) ;
PrintWriter printWriter = new PrintWriter ( new FileWriter ( "D:\\123.xml" ) ) ;
String listName = "" ;
transToXML ( insurance, printWriter, listName) ;
printWriter. close ( ) ;
}
public static void transToXML ( Object obj, PrintWriter printWriter, String listName) throws Exception {
Field[ ] fields = obj. getClass ( ) . getDeclaredFields ( ) ;
Object o = null;
Class< ? > genericClazz = null;
for ( Field field: fields) {
if ( ! field. isAccessible ( ) ) {
field. setAccessible ( true ) ;
}
if ( ! List. class . isAssignableFrom ( field. getType ( ) ) ) {
printWriter. println ( "<" + field. getName ( ) + ">${" + field. getName ( ) + "}</" + field. getName ( ) + ">" ) ;
} if ( List. class . isAssignableFrom ( field. getType ( ) ) ) {
Type genericType = field. getGenericType ( ) ;
if ( genericType == null) continue ;
if ( genericType instanceof ParameterizedType ) {
ParameterizedType pt = ( ParameterizedType) genericType;
genericClazz = ( Class< ? > ) pt. getActualTypeArguments ( ) [ 0 ] ;
printWriter. println ( "<" + genericClazz. getSimpleName ( ) . substring ( 0 , 1 ) . toLowerCase ( ) + genericClazz. getSimpleName ( ) . substring ( 1 ) + "List>" ) ;
o = genericClazz. newInstance ( ) ;
transToXML ( o, printWriter, listName) ;
listName = "</" + genericClazz. getSimpleName ( ) . substring ( 0 , 1 ) . toLowerCase ( ) + genericClazz. getSimpleName ( ) . substring ( 1 ) + "List>" ;
System. out. println ( listName) ;
printWriter. println ( listName) ;
}
}
}
}