自定义 JAXB xml 输出

发布于 2024-11-15 17:22:38 字数 852 浏览 0 评论 0原文

给定以下类:

public class Customer {
  public String name;
  public String lastName;
}

我想使用 JAXB 为 name 为 John、lastName 为 Doe 的客户生成以下 xml 输出:

<cst>John Doe</cst>

我如何使用 JAXB 执行此操作?

编辑

Customer在多个地方使用,如下所示:

public class Sale {
  private String productId;
  private Date date;
  private Customer customer;
}

public class Transaction {
  private List<Sale> sales;
}

...等等...问题是,如何才能我告诉 JAXB:“每当您见到客户时,请使用自定义格式”?

我的问题是有很多类包含客户,我想以编程方式控制输出(有时 name + lastname,有时 namelastname),而无需在包含 Customer 的每个类中添加注释。此要求将排除使用 JAXBElement

Given the following class:

public class Customer {
  public String name;
  public String lastName;
}

I want to generate the following xml output using JAXB for a customer whose name is John and lastName is Doe:

<cst>John Doe</cst>

How can i do this with JAXB?

EDIT

The class Customer is used in several places, as shown here:

public class Sale {
  private String productId;
  private Date date;
  private Customer customer;
}

public class Transaction {
  private List<Sale> sales;
}

... and so on... The deal is, how can I tell JAXB: "whenever you see a customer, please use custom formatting"?

My problem is that there are many classes that contain a customer, and I want to programatically control the output (sometimes name + lastname, sometimes <name>name</name>, <lastname>lastname</lastname>) without adding annotations at every class that contains Customer. This requirement would rule out using JAXBElement<Customer>.

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

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

发布评论

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

评论(1

﹉夏雨初晴づ 2024-11-22 17:22:38

您可以安装一个处理翻译的XmlAdapter

public static void main(String[] args) throws Exception {

    JAXBContext ctxt = JAXBContext.newInstance(CustomerWrapper.class);
    Marshaller m = ctxt.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Customer customer = new Customer("John", "Doe");
    m.marshal(new JAXBElement<CustomerWrapper>(new QName("cwrapper"), CustomerWrapper.class, new CustomerWrapper(customer)), System.err);

}

static class CustomerWrapper {
    private Customer customer;

    public CustomerWrapper() {
    }

    public CustomerWrapper(Customer customer) {
        this.customer = customer;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

@XmlJavaTypeAdapter(CustomerAdapter.class)
static class Customer {
    private String name;
    private String lastName;
    public Customer() {
    }
    public Customer(String name, String lastName) {
        this.name = name;
        this.lastName = lastName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

static class CustomerAdapter extends XmlAdapter<String, Customer> {

    @Override
    public Customer unmarshal(String v) throws Exception {
        String[] ss = v.split(" ");
        return new Customer(ss[0], ss[1]);
    }

    @Override
    public String marshal(Customer v) throws Exception {
        return v.getName() + " " + v.getLastName();
    }

}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cwrapper>
    <customer>John Doe</customer>
</cwrapper>

You could install an XmlAdapter that handles the translation:

public static void main(String[] args) throws Exception {

    JAXBContext ctxt = JAXBContext.newInstance(CustomerWrapper.class);
    Marshaller m = ctxt.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Customer customer = new Customer("John", "Doe");
    m.marshal(new JAXBElement<CustomerWrapper>(new QName("cwrapper"), CustomerWrapper.class, new CustomerWrapper(customer)), System.err);

}

static class CustomerWrapper {
    private Customer customer;

    public CustomerWrapper() {
    }

    public CustomerWrapper(Customer customer) {
        this.customer = customer;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

@XmlJavaTypeAdapter(CustomerAdapter.class)
static class Customer {
    private String name;
    private String lastName;
    public Customer() {
    }
    public Customer(String name, String lastName) {
        this.name = name;
        this.lastName = lastName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

static class CustomerAdapter extends XmlAdapter<String, Customer> {

    @Override
    public Customer unmarshal(String v) throws Exception {
        String[] ss = v.split(" ");
        return new Customer(ss[0], ss[1]);
    }

    @Override
    public String marshal(Customer v) throws Exception {
        return v.getName() + " " + v.getLastName();
    }

}

outputs

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cwrapper>
    <customer>John Doe</customer>
</cwrapper>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文