使用jaxb进行xml到bean的转换(尝试解决空值不显示问题)

import javax.xml.bind.Marshaller;
import java.lang.reflect.Field;
//监听生成xml文件过程
public class MarshallerListener extends Marshaller.Listener {

    public static final String BLANK_CHAR = "";

    @Override
    public void beforeMarshal(Object source) {
        super.beforeMarshal(source);
        Field[] fields = source.getClass().getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            try {
                System.out.println(f.getType() + f.getName());
                if (f.getType() == String.class && f.get(source) == null) {
                    f.set(source, BLANK_CHAR);
                }else if(!isBaseType(f)&& f.get(source) != null){
                    beforeMarshalClass(f.get(source));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    
    private void beforeM

你可能感兴趣的:(java日常)