Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定制的jaxbxml输出_Java_Xml_Jaxb - Fatal编程技术网

Java 定制的jaxbxml输出

Java 定制的jaxbxml输出,java,xml,jaxb,Java,Xml,Jaxb,鉴于以下类别: public class Customer { public String name; public String lastName; } 我想使用JAXB为name是John且lastName是Doe的客户生成以下xml输出: <cst>John Doe</cst> 。。。等等问题是,我如何告诉JAXB:“无论何时看到客户,请使用自定义格式” 我的问题是有许多类包含客户,我希望通过编程控制输出(有时name+lastname,有时name,l

鉴于以下类别:

public class Customer {
  public String name;
  public String lastName;
}
我想使用JAXB为
name
是John且
lastName
是Doe的客户生成以下xml输出:

<cst>John Doe</cst>
。。。等等问题是,我如何告诉JAXB:“无论何时看到客户,请使用自定义格式”


我的问题是有许多类包含客户,我希望通过编程控制输出(有时
name+lastname
,有时
name
lastname
),而不在包含
customer
的每个类中添加注释。此要求将排除使用
JAXBElement

您可以安装一个
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();
    }

}
publicstaticvoidmain(字符串[]args)引发异常{
JAXBContext ctxt=JAXBContext.newInstance(CustomerWrapper.class);
Marshaller m=ctxt.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式的_输出,Boolean.TRUE);
客户=新客户(“约翰”、“能源部”);
m、 marshal(新JAXBElement(新QName(“cwrapper”)、CustomerWrapper.class、新CustomerWrapper(customer))、System.err;
}
静态类CustomerRapper{
私人客户;
公共CustomerRapper(){
}
公共客户说话人(客户){
this.customer=customer;
}
公共客户getCustomer(){
退货客户;
}
公共作废设置客户(客户){
this.customer=customer;
}
}
@XmlJavaTypeAdapter(CustomerAdapter.class)
静态类客户{
私有字符串名称;
私有字符串lastName;
公众客户(){
}
公共客户(字符串名称、字符串姓氏){
this.name=名称;
this.lastName=lastName;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
this.lastName=lastName;
}
}
静态类CustomerAdapter扩展了XmlAdapter{
@凌驾
公共客户解组(字符串v)引发异常{
字符串[]ss=v.split(“”);
返回新客户(ss[0],ss[1]);
}
@凌驾
公共字符串封送处理(客户v)引发异常{
返回v.getName()+“”+v.getLastName();
}
}
输出

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

无名氏

谢谢你的回答,这让我离目标更近了。。。你能看一下我的编辑吗?我把我原来的问题简单化了。谢谢这是一个奇怪的要求。您可以为每种编组类型创建
Customer
的子类,并为每种编组类型提供
XmlAdapter
,但我不知道这是否有效。事实证明,您的解决方案是现成的。。。使用@XmlAdapter可以实现神奇的效果。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cwrapper>
    <customer>John Doe</customer>
</cwrapper>