JAXB为简单数据类型向XmlElement添加属性

时间:2011-05-06 17:42:08

标签: java annotations jaxb

我想在从JavaBeans编组时使用JAXB向Xml Elements添加一些属性。 Xml元素是简单的数据类型,如String。所以我不想创建新的类。例如,期望的输出将是:

<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

2 个答案:

答案 0 :(得分: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来删除不同类的需要

答案 1 :(得分:2)

您可以使用EclipseLink JAXB (MOXy)的@XmlPath注释来解决此问题(我是MOXy技术主管):

通知

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

要将MOXy用作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);
    }

}

更多信息: