【问题标题】:Add custom values to external binding (JAXB - MOXY implementation)将自定义值添加到外部绑定(JAXB - MOXY 实现)
【发布时间】:2013-10-28 17:58:56
【问题描述】:

我想将 Java 对象编组为 XML。对于这种方法,我将 JAXB Moxy 与外部 XML 绑定文件一起使用。

这是一个我想要编组为 XML 的示例类:

public class Customer {
  private String lastname;
  private String firstname;

  //getters and setters
}

通过我的外部绑定文件,我可以访问这个类的值,从而获得以下 XML 输出:

<?xml version="1.0"?>
<customer>
  <firstname>Tony</firstname>
  <lastname>Stark</lastname>
</customer>

现在我想添加一个带有自定义值的自定义标记,该值未在 java 类中指定。所以对于上面的 Customer 类,我想要一个这样的 XML 输出:

<?xml version="1.0"?>
<customer>
  <firstname>Tony</firstname>
  <lastname>Stark</lastname>
  <birthdate>01.01.1990</birthdate>
</customer>

Birthdate 不在 Customer 类中,我不想在其中添加它,因为该类是由脚本自动生成的。我的目标是在外部绑定文件中用我的自定义值定义生日。这甚至可以通过 JAXB MOXY 实现实现吗? 希望有人可以帮助我。

【问题讨论】:

    标签: java xml jaxb moxy


    【解决方案1】:

    以下是您可以利用XmlAdapter 执行此操作的方法:

    XmlAdapter (LastNameAdapter)

    import javax.xml.bind.annotation.XmlType;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class LastNameAdapter extends XmlAdapter<LastNameAdapter.AdaptedLastName, String> {
    
        @XmlType(propOrder={"lastname", "birthdate"})
        public static class AdaptedLastName {
            public String lastname;
            public String birthdate;
        }
    
        private String birthdate;
    
        public LastNameAdapter() {
        }
    
        public LastNameAdapter(String birthdate) {
            this.birthdate = birthdate;
        }
    
        @Override
        public String unmarshal(AdaptedLastName v) throws Exception {
            return v.lastname;
        }
    
        @Override
        public AdaptedLastName marshal(String v) throws Exception {
            AdaptedLastName adaptedLastName = new AdaptedLastName();
            adaptedLastName.lastname = v;
            adaptedLastName.birthdate = birthdate;
            return adaptedLastName;
        }
    
    }
    

    外部元数据 (oxm.xml)

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum19641824">
        <java-types>
            <java-type name="Customer">
                <xml-root-element/>
                <xml-type prop-order="firstname lastname"/>
                <java-attributes>
                    <xml-element java-attribute="firstname"/>
                    <xml-element java-attribute="lastname" xml-path=".">
                        <xml-java-type-adapter value="forum19641824.LastNameAdapter"/>
                    </xml-element>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    演示

    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19641824/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
    
            Customer customer = new Customer();
            customer.setFirstname("Tony");
            customer.setLastname("Stark");
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setAdapter(new LastNameAdapter("01.01.1990"));
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(customer, System.out);
        }
    
    }
    

    输出

    <?xml version="1.0" encoding="UTF-8"?>
    <customer>
       <firstname>Tony</firstname>
       <lastname>Stark</lastname>
       <birthdate>01.01.1990</birthdate>
    </customer>
    

    【讨论】:

    • 谢谢。这就是我一直在寻找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2021-02-13
    • 2013-11-07
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多