2011-05-06 92 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