使用jaxb生成xml时空对象的空标记

时间:2013-12-09 11:12:05

标签: jaxb

我已经为编组创建了两个类。虽然使用jaxb为xmlb生成xml,但我为空的对象获取空标记。如何克服它?

@XmlType(propOrder = {"lastName","raceEthnicity"}
public class PatientIdentifier {
@XmlElement(name = "lastName")
private String lastName;
private RaceEthinicity raceEthnicity; 
public PatientIdentifier(){}
public PatientIdentifier(String lastName,RaceEthinicity raceEthnicity) {
this.lastName = lastName;
this.raceEthnicity = raceEthnicity;
}

@XmlElement(name = "raceEthnicity",type = RaceEthinicity.class) 
public RaceEthinicity getRaceEthnicity() {
if(null != raceEthnicity )
{
  return (RaceEthinicity) StringUtil.getRequiredValues(raceEthnicity);
}
else
{
  return null;
}
}

}

以下是RaceEthnicity Class

@XmlType(propOrder = { "race","ethnicity"})

public class RaceEthinicity {

@XmlAttribute
private String selfReported;
private Race race;
@XmlElement(name = "ethnicity") 
private String ethnicity;

public void setSelfReported(String selfReported) {
    this.selfReported = selfReported;
}

public RaceEthinicity(){}

public RaceEthinicity(Race race, String ethnicity) {
this.race = race;
this.ethnicity = ethnicity;
}

/*@XmlElement(name = "ethnicity")
public String getEthnicity() {
return ethnicity;
}*/

@XmlElement(name = "race")
public Race getRace() {
if(race!=null)
{   
 return (Race) StringUtil.getRequiredValues(race);
}
else
{
return null;
}
}



}

种族等级如下

@XmlType(propOrder = { "raceCode","tribeCode"})
public class Race {

@XmlElement(name = "raceCode")
private String raceCode;

@XmlElement(name = "tribeCode") 
private String tribeCode;

public Race(){}

public Race(String raceCode, String tribeCode) {
    this.raceCode = raceCode;
    this.tribeCode = tribeCode;
}

public String getRaceCode() {
    return raceCode;
}   

}

输出xml im getting如下

<patientIdentifier>
 <lastName>ADAME</lastName>
 <raceEthnicity/>
</patientIdentifier>

我想更改我的代码,以便当raceEthnicity为null时,不应该使用xml。

1 个答案:

答案 0 :(得分:0)

JAXB (JSR-222)实现不会为空值输出空元素。根据您的域模型,运行以下演示代码(注释null作为第二个参数传递给构造函数):

import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(PatientIdentifier.class);

        PatientIdentifier pi = new PatientIdentifier("ADAME", null);
        JAXBElement<PatientIdentifier> jaxbElement = new JAXBElement<PatientIdentifier>(new QName("patientIdentifier"), PatientIdentifier.class, pi);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, System.out);
    }

}

将产生以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<patientIdentifier>
    <lastName>ADAME</lastName>
</patientIdentifier>

注意:我在StringUtil类中使用了以下内容:

import java.lang.reflect.Field;

public class StringUtil {

    public static Object getRequiredValues(Object obj) {
        boolean allChildsAreNull = true;
        if (null != obj) {
            for (Field f : obj.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                try {
                    if (f.get(obj) != null) {
                        allChildsAreNull = false;
                        break;
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return allChildsAreNull ? null : obj;
    }

}