2010-12-15 70 views
4

我正在使用JAXB2將對象序列化爲xml。Java xml序列化null處理

有什麼辦法如何強制它創建整個對象結構,就像在下面的例子中一樣,即使它沒有填充到後備對象中?

即使沒有asignee屬性集,這也是我的預期結果。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<note> 
    <to xsi:nil="true"/> 
    <from xsi:nil="true"/> 
    <header xsi:nil="true"/> 
    <body>text</body> 
    <assignee> 
     <name xsi:nil="true"/> 
     <surname xsi:nil="true"/> 
    </assignee> 
</note> 

我用下面的代碼序列:

JAXBContext jc = JAXBContext.newInstance(dataObject.getClass()); 
Marshaller marshaller = jc.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); 
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset); 
marshaller.marshal(dataObject, outputStream); 

回答

3

你可以做到以下幾點:

注意

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlRootElement 
@XmlType(propOrder={"to", "from", "header", "body", "assignee"}) 
public class Note { 

    private String to; 
    private String from; 
    private String header; 
    private String body; 
    private Assignee assignee; 

    @XmlElement(nillable=true) 
    public String getTo() { 
     return to; 
    } 

    public void setTo(String to) { 
     this.to = to; 
    } 

    @XmlElement(nillable=true) 
    public String getFrom() { 
     return from; 
    } 

    public void setFrom(String from) { 
     this.from = from; 
    } 

    @XmlElement(nillable=true) 
    public String getHeader() { 
     return header; 
    } 

    public void setHeader(String header) { 
     this.header = header; 
    } 

    @XmlElement(nillable=true) 
    public String getBody() { 
     return body; 
    } 

    public void setBody(String body) { 
     this.body = body; 
    } 

    public Assignee getAssignee() { 
     return assignee; 
    } 

    public void setAssignee(Assignee assignee) { 
     this.assignee = assignee; 
    } 

} 

受讓人

我們需要有否當受讓人的解組實例應被解釋爲空的手段。我添加了一個isNull()方法,如果所有字段都爲空,則返回true。

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

@XmlJavaTypeAdapter(AssigneeAdapter.class) 
public class Assignee { 

    private String name; 
    private String surname; 

    @XmlElement(nillable=true) 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @XmlElement(nillable=true) 
    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public boolean isNull() { 
     return null == name && null == surname; 

    } 
} 

AssigneeAdapter

的AssigneeAdapter同時使用該值類型和綁定類型受讓人對象。這個類利用我們增加對受讓人的ISNULL()方法:

import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class AssigneeAdapter extends XmlAdapter<Assignee, Assignee> { 

    @Override 
    public Assignee unmarshal(Assignee v) throws Exception { 
     if(v.isNull()) { 
      return null; 
     } 
     return v; 
    } 

    @Override 
    public Assignee marshal(Assignee v) throws Exception { 
     if(null == v) { 
      return new Assignee(); 
     } 
     return v; 
    } 

} 

演示

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

public class Demo { 

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

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

有關XmlAdapter更多信息,請參閱:

+0

非常感謝!看起來像@XmlJavaTypeAdapter是我的一天。 – 2010-12-16 07:14:56

0
+0

當您需要區分爲什麼您的值爲null(缺少xsi:nil =「true」時顯示的元素或元素)時,JAXBElement非常有用。如果你總是想用xsi:nil =「true」來表示null,那麼你可以使用@XmlElement(nillable = true)。看看我的答案:http://stackoverflow.com/questions/4449284/java-xml-serialization-null-handling/4455014#4455014 – 2010-12-15 21:28:07