2011-06-16 40 views
2
BufferedWriter out = new BufferedWriter(fstream); 
    try { 
     JAXBContext context = JAXBContext.newInstance(NarociloType.class); 

     Marshaller m = context.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     m.marshal(parameters, out); 
     out.newLine(); 
    } catch (PropertyException pe) { 
     // TODO: Add catch code 
     pe.printStackTrace(); 
    } catch (JAXBException jaxbe) { 
     // TODO: Add catch code 
     jaxbe.printStackTrace(); 
    } 

但空類型不存儲到XML中。例如:使用JAXBContext和Marshaller將對象與XML和XML對象

NarociloType.date = null 

但我不能在xml中看到<date></date>。 JAXB編組不會爲空值創建空元素

我還可以使用JAXBContext更改XML以反對對象嗎?

回答

1

注:下面的例子中EclipseLink JAXB (MOXy)工作,但包含在Java SE 6

不是JAXB參考實現如果使用莫西爲您的JAXB提供者(我是技術主管) ,那麼你可以在這個用例中使用XmlAdapter

DateAdatper

import java.util.Date; 

import javax.xml.bind.annotation.XmlValue; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
import forum235.DateAdapter.AdaptedDate; 

public class DateAdapter extends XmlAdapter<AdaptedDate, Date> { 

    @Override 
    public Date unmarshal(AdaptedDate adaptedDate) throws Exception { 
     return adaptedDate.date; 
    } 

    @Override 
    public AdaptedDate marshal(Date date) throws Exception { 
     AdaptedDate adaptedDate = new AdaptedDate(); 
     adaptedDate.date = date; 
     return adaptedDate; 
    } 

    static class AdaptedDate { 
     @XmlValue 
     public Date date; 
    } 

} 

import java.util.Date; 

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

@XmlRootElement 
public class Root { 

    private Date date; 

    @XmlJavaTypeAdapter(DateAdapter.class) 
    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

} 

演示

import java.util.Date; 

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(Root.class); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     Root root = new Root(); 
     marshaller.marshal(root, System.out); 

     root.setDate(new Date()); 
     marshaller.marshal(root, System.out); 
    } 

} 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <date/> 
</root> 
<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <date>2011-06-16T09:16:09.452</date> 
</root> 

jaxb.properties

您使用莫西爲您的JAXB提供者需要提供一個名爲在同一個包jaxb.properties文件作爲域類具有以下條目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

有關XmlAdapter更多信息

+0

嗨,thx的答案,但我使用JDeveloper 11g,如果我仍然使用jaxb.properties使用舊的javax.xml.bind.JAXBContext; – senzacionale 2011-06-20 05:43:06

+0

@senzacionale - 您將需要包含eclipselink.jar文件。你可以在這裏獲得一個:http://www.eclipse。組織/的EclipseLink /下載/ – 2011-06-20 20:42:08

0
+0

THX,但我有,我想將其轉換爲XML,如果是null,則不會被保存爲XML – senzacionale 2011-06-16 06:37:25

+0

同樣的問題對象類型這裏http://metro.1045641.n5.nabble.com/JAXB-marshalling-does-not-create-empty-element-for-null-values-td3073245.html沒有回答 – senzacionale 2011-06-16 06:39:08