我见过很多JAXB实现,我们可以使用@XmlElement
注释将java原语转换为XML元素。
但是,我想将以下POJO转换为XML(注意员工类中有一个地址 object 而不仅仅是原语):
public class Employee {
private Address address;
private int employeeId;
// constructors + setters + getters
}
如何使用这些JAXB注释将员工对象编组为XML?
感谢。
答案 0 :(得分:17)
你需要做的就是编组一个POJO属性与编组一个原始属性。引用的POJO类不需要使用@XmlRootElement
进行注释。
<强>员工强>
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
private Address address;
private int employeeId;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
}
<强>地址强>
将Address
作为Employee
的一部分进行编组,您无需做任何特殊工作。
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
下面是一些演示代码,它将填充和员工模型并将其编组为XML。
<强>演示强>
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Employee.class);
Address address = new Address();
address.setStreet("1 A Street");
Employee employee = new Employee();
employee.setEmployeeId(123);
employee.setAddress(address);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
}
<强>输出强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<address>
<street>1 A Street</street>
</address>
<employeeId>123</employeeId>
</employee>
如果要覆盖默认元素名称,则可以使用@XmlElement
注释,无论属性的类型如何。
<强>员工强>
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Employee {
private Address address;
private int employeeId;
@XmlElement(name="ADDR")
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@XmlElement(name="ID")
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
}
<强>输出强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<ADDR>
<street>1 A Street</street>
</ADDR>
<ID>123</ID>
</employee>
答案 1 :(得分:0)
使用Jaxb,您可以尝试以下代码。或者,您可以尝试Xstream
@XmlRootElement
public class TestObject {
String a;
TestObject1 anotherObject;
public String getA() {
return a;
}
@XmlElement
public void setA(String a) {
this.a = a;
}
public TestObject1 getAnotherObject() {
return anotherObject;
}
@XmlElement
public void setAnotherObject(TestObject1 anotherObject) {
this.anotherObject = anotherObject;
}
public static void main(String[] args) throws JAXBException {
TestObject object = new TestObject();
object.setA("A");
TestObject1 anotherObject = new TestObject1();
anotherObject.setB("B");
object.setAnotherObject(anotherObject);
File file = new File("output.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(TestObject.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(object, file);
jaxbMarshaller.marshal(object, System.out);
}
}
========================
@XmlRootElement
public class TestObject1 {
String b;
public String getB() {
return b;
}
@XmlElement
public void setB(String b) {
this.b = b;
}
}
答案 2 :(得分:0)
我能够在对象内实现对象&#34;通过下面给出的appraoch(即通过使用@XmlRootElement
注释注释两个类)对JAXB进行XML编组:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
@XmlElement
private Address address;
.
.
}
@XmlRootElement
public class Address {
.
.
}