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

发布于 2024-11-06 01:15:38 字数 414 浏览 0 评论 0原文

我想在从 JavaBean 编组时使用 JAXB 向 Xml 元素添加一些属性。 Xml 元素是像字符串这样的简单数据类型。所以我不想创建新的类。例如,所需的输出是:

<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

我不想将主题和正文定义为单独的类。

-阿南德

I want to add some attributes to Xml Elements using JAXB when marshalling from JavaBeans. The Xml Elements are simple data types like String. So I do not want to create new Classes. For example, a desired output would be:

<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

I do not want to define subject and body as separate classes.

-Anand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-11-13 01:15:38

我的解决方案需要为主题和正文定义一个类,但所需的输出将按照要求
我使用@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来消除对不同类的需要

My solution require defining a class for subject and body, but the desired output will be as requested
I use @XmlValue for the message and @XmlAttribute for the attributes

@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;
}

NOTE:I only wrote the subject part, I wonder if using @XmlPath can be used to remove the need for different classes

半世蒼涼 2024-11-13 01:15:38

您可以使用 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);
    }

}

了解更多信息:

You could use EclipseLink JAXB (MOXy)'s @XmlPath annotation to solve this problem (I'm the MOXy tech lead):

Notifications

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

To use MOXy as your JAXB implementation you need to put a file named jaxb.properties in the same package as your model classes with the following entry:

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

Demo

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);
    }

}

For More Information:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文