如何使用JAXB自定义XML导出

出于测试目的,我使用了使用JAXB从Object生成XML。 这工作很好。 代码如下。

package com.mns.mnsutilities.jaxb.model; import java.util.List; import javax.xml.bind.annotation.*; @XmlRootElement(name="Emp_MNS") @XmlType(propOrder= {"name", "age", "role", "gender", "addressesList"}) public class Employee { private int id; private String gender; private int age; private String name; private String role; private String password; private List
addressesList; public Employee() {} public Employee(int id, String gender, int age, String name, String role, String password) { super(); this.id = id; this.gender = gender; this.age = age; this.name = name; this.role = role; this.password = password; } public Employee(int id, String gender, int age, String name, String role, String password, List
addressesList) { super(); this.id = id; this.gender = gender; this.age = age; this.name = name; this.role = role; this.password = password; this.addressesList = addressesList; } @XmlAttribute public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement(name = "gen") public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } // @XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE) @XmlElement(nillable=true) public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @XmlTransient public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @XmlElement(name = "addresses") public List
getAddressesList() { return addressesList; } public void setAddressesList(List
addressesList) { this.addressesList = addressesList; } @Override public String toString() { return "Employee [id=" + id + ", gender=" + gender + ", age=" + age + ", name=" + name + ", role=" + role + ", password=" + password + ", addressesList=" + addressesList + "]"; } }

和:

  package com.mns.mnsutilities.jaxb.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(namespace="") public class Address { private String street; private String city; private String zipCode; private String country; public Address() {} public Address(String street, String city, String zipCode, String country) { super(); this.street = street; this.city = city; this.zipCode = zipCode; this.country = country; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "Address [street=" + street + ", city=" + city + ", zipCode=" + zipCode + ", country=" + country + "]"; } } 

我的主要课程是:

  package com.mns.mnsutilities.jaxb.batch; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import com.mns.mnsutilities.jaxb.model.Address; import com.mns.mnsutilities.jaxb.model.Employee; public class LaunchAction { private static final String FILE_NAME = "output/CT3D_XML_SAMPLE_FINAL.xml"; public static void main(String[] args) { Employee emp = new Employee(); emp.setId(1); emp.setAge(25); emp.setName("Yovan"); emp.setGender("Male"); emp.setRole("Developer"); emp.setPassword("sensitive"); List
addressesList = new ArrayList(); Address address1 = new Address("Main Road", "Ebene", "11111", "Mauritius"); Address address2 = new Address("Royal Road", "Rose-Hill", "2222", "Mauritius"); addressesList.add(address1); addressesList.add(address2); emp.setAddressesList(addressesList); jaxbObjectToXML(emp); Employee empFromFile = jaxbXMLToObject(); System.out.println(empFromFile.toString()); } private static Employee jaxbXMLToObject() { try { JAXBContext context = JAXBContext.newInstance(Employee.class); Unmarshaller un = context.createUnmarshaller(); Employee emp = (Employee) un.unmarshal(new File(FILE_NAME)); return emp; } catch (JAXBException e) { e.printStackTrace(); } return null; } private static void jaxbObjectToXML(Employee emp) { try { JAXBContext context = JAXBContext.newInstance(Employee.class); Marshaller m = context.createMarshaller(); //for pretty-print XML in JAXB m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // Write to System.out for debugging m.marshal(emp, System.out); // Write to File m.marshal(emp, new File(FILE_NAME)); } catch (JAXBException e) { e.printStackTrace(); } } }

XML输出是:

      25 Developer Juggoo  Ebene Mauritius Main Road 11111   Rose-Hill Mauritius Royal Road 2222   

我真正想要的是:

      25 Developer Juggoo  **
** Ebene Mauritius Main Road 11111 **
** **
** Rose-Hill Mauritius Royal Road 2222 **
**

你能指导我如何继续吗?

您可以执行以下操作:

 @XmlElementWrapper(name="addresses") @XmlElement(name="address") public List
getAddressesList() {