2011-05-06 95 views
3

我想在JavaBeans編組時使用JAXB向Xml元素添加一些屬性。 Xml元素是簡單的數據類型,如String。所以我不想創建新的類。例如,期望的輸出將是:JAXB爲簡單數據類型添加屬性到XmlElement

<notifications> 
<date>04/20/2011</date> 
<subject creditcard_num="22678" checknum="8904">Credit Card Charge Back</subject> 
<body payment_amount="34.00" return_status="charged back">some text</body> 
</notifications 

我不想將主體和主體定義爲單獨的類。

-Anand

+0

你爲什麼不想爲主體和主體定義類?你可以做到這一點,仍然有所需的輸出相同 – ekeren 2011-05-06 19:07:09

回答

2

您可以使用EclipseLink JAXB (MOXy)的@XmlPath註釋來解決這個問題(我是莫西技術主管):

通知

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 

import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Notifications { 

    private String date; 

    @XmlPath("subject/@creditcard_num") 
    private String creditcardNum; 

    @XmlPath("subject/@checknum") 
    private String checknum; 

    private String subject; 

    @XmlPath("body/@payment_amount") 
    private String paymentAmount; 

    @XmlPath("body/@return_status") 
    private String returnStatus; 

    private String body; 

} 

jaxb.properties

要使用莫西爲您的JAXB實現您需要將一個名爲jaxb.properties的文件放入與您的模型類相同的包中,並使用以下條目:

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

演示

import java.io.File; 

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

public class Demo { 

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

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     Notifications notifications = (Notifications) unmarshaller.unmarshal(new File("input.xml")); 

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

} 

更多信息:

+0

有沒有辦法使用'@ XmlPath'重命名這些屬性?例如,我希望將creditcardNum屬性重命名爲「value」,並將屬性paymentAmount重命名爲「value」。我無法更改變量的名稱,因爲變量名必須是唯一的。 – 2016-04-10 12:35:48

6

我的解決辦法需要限定用於主題和正文的一類,但所期望的輸出將是所要求 我使用@XmlValue用於消息和@XmlAttribute爲屬性

@Test 
public void testAll() throws JAXBException 
{ 
    String msg = "<notifications><date>04/20/2011</date><subject creditcard_num='22678' checknum='8904'>Credit Card Charge Back</subject><body payment_amount='34.00' return_status='charged back'>some text</body></notifications>"; 
    Notifications tested = (Notifications) JAXBContext.newInstance(Notifications.class).createUnmarshaller().unmarshal(new StringReader(msg)); 
    assertEquals("Credit Card Charge Back",tested.subject.value); 
    assertEquals("8904",tested.subject.checknum); 
    assertEquals("22678",tested.subject.creditcard_num); 
} 
@XmlRootElement 
public static class Notifications{ 
    public String date; 
    public Subject subject; 
} 

public static class Subject 
{ 
    @XmlValue 
    public String value; 

    @XmlAttribute(name="creditcard_num") 
    public String creditcard_num; 

    @XmlAttribute(name="checknum") 
    public String checknum; 
} 

備註:我只寫了主題部分,不知是否使用@XmlPath可以用來刪除不同類別的需要

+0

是的@XmlPath註釋將刪除不同類的需要:http://stackoverflow.com/questions/5915038/jaxb-adding-attributes-toa-a- xmlelement-for-simple-data-types/5916476#5916476 – 2011-05-06 20:11:43