有没有一个很好的方法来转换Java到XML与给定的约束?
此外,认为一个工具可能是有帮助的,但我会更多intersted它有一些Java API做同样的事情.
解决方法
域模型
我将使用以下域模型作为此答案.注意模型上没有JAXB注释.
顾客
package forum11693552; import java.util.*; public class Customer { private String firstName; private String lastName; private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>(); public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public List<PhoneNumber> getPhoneNumbers() { return phoneNumbers; } public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) { this.phoneNumbers = phoneNumbers; } }
电话号码
package forum11693552; public class PhoneNumber { private String type; private String number; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
选项#1 – 任何JAXB(JSR-222)实现
JAXB是由异常组态,这意味着您只需要添加注释,您希望映射行为与默认值不同.以下是一个示例的链接,演示如何使用任何没有注释的JAXB impl:
演示
package forum11693552; import javax.xml.bind.*; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Customer.class); Customer customer = new Customer(); customer.setFirstName("Jane"); customer.setLastName("Doe"); PhoneNumber workPhone = new PhoneNumber(); workPhone.setType("work"); workPhone.setNumber("555-1111"); customer.getPhoneNumbers().add(workPhone); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"),Customer.class,customer); marshaller.marshal(rootElement,System.out); } }
产量
<customer> <firstName>Jane</firstName> <lastName>Doe</lastName> <phoneNumbers> <number>555-1111</number> <type>work</type> </phoneNumbers> </customer>
了解更多信息
> http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics
选项#2 – EclipseLink JAXB(MOXy)的外部映射文档
如果您想自定义映射,那么您可能对MOXy的外部映射文档扩展感兴趣.样本映射文档如下所示:
oxm.xml
<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum11693552"> <java-types> <java-type name="Customer"> <xml-root-element /> <java-attributes> <xml-element java-attribute="firstName" name="first-name" /> <xml-element java-attribute="lastName" name="last-name" /> <xml-element java-attribute="phoneNumbers" name="phone-number" /> </java-attributes> </java-type> <java-type name="PhoneNumber"> <java-attributes> <xml-attribute java-attribute="type" /> <xml-value java-attribute="number" /> </java-attributes> </java-type> </java-types> </xml-bindings>
jaxb.properties
要使MOXy成为您的JAXB提供商,您需要在与您的域模型相同的软件包中包含一个名为jaxb.properties的文件,并具有以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
当使用EclipseLink MOXy作为JAXB提供程序(见)时,可以在引导JAXBContext时利用外部映射文档
package forum11693552; import java.util.*; import javax.xml.bind.*; import javax.xml.namespace.QName; import org.eclipse.persistence.jaxb.JAXBContextFactory; public class Demo { public static void main(String[] args) throws Exception { Map<String,Object> properties = new HashMap<String,Object>(1); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY,"forum11693552/oxm.xml"); JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class},properties); Customer customer = new Customer(); customer.setFirstName("Jane"); customer.setLastName("Doe"); PhoneNumber workPhone = new PhoneNumber(); workPhone.setType("work"); workPhone.setNumber("555-1111"); customer.getPhoneNumbers().add(workPhone); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,System.out); } }
产量
<?xml version="1.0" encoding="UTF-8"?> <customer> <first-name>Jane</first-name> <last-name>Doe</last-name> <phone-number type="work">555-1111</phone-number> </customer>
了解更多信息
> http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
> http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html