2013-12-09 88 views
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。

回答

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; 
    } 

} 
+0

你好感謝你的是如下圖所示公共靜態對象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

+0

我是否需要在這裏修改任何東西 – user3082430

+0

@ user3082430 - 我修改了我的'getRequiredValues'方法以匹配您的方法並獲得與之前相同的輸出。當你從我的答案中運行代碼時,你會得到什麼? –