1
我已經創建了兩個編組類。當使用jaxb生成xml時,我得到的空標記爲null。如何克服它?使用jaxb生成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類
@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即時得到如下面
<patientIdentifier>
<lastName>ADAME</lastName>
<raceEthnicity/>
</patientIdentifier>
我想改變我的代碼讓raceEt如果它爲空,那麼hnicity不應該進入xml。
你好感謝你的是如下圖所示公共靜態對象g solution.But我在我的stringutil類getRequiredValues etRequiredValues(final Object obj){ \t \t boolean allChildsAreNull = true; \t \t \t如果(空= OBJ!){ \t \t \t爲(場F:obj.getClass()getDeclaredFields()){ \t \t \t \t f.setAccessible(真); \t \t \t \t嘗試{ \t \t \t \t \t如果(f.get(OBJ)!= NULL){ \t \t \t \t \t \t allChildsAreNull = FALSE; \t \t \t \t \t \t break; \t \t \t \t \t} \t \t \t \t}趕上(拋出:IllegalArgumentException E){ \t \t \t \t \t e.printStackTrace(); \t \t \t \t}趕上(IllegalAccessException E){ \t \t \t \t \t即的printStackTrace(); \t \t \t \t} \t \t \t} \t} \t \t回報allChildsAreNull? null:obj; \t} – user3082430
我是否需要在這裏修改任何東西 – user3082430
@ user3082430 - 我修改了我的'getRequiredValues'方法以匹配您的方法並獲得與之前相同的輸出。當你從我的答案中運行代碼時,你會得到什麼? –