[英]Generating attribute option in xml using JAXB
I have to generate the following xml in my program using JAXB. 我必须使用JAXB在我的程序中生成以下xml。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Employee empId="12345">
<name>ABC</name>
<address type="Residence">Bangalore</address>
</Employee>
I have to generate the above xml using JAXB. 我必须使用JAXB生成上面的xml。 I am having the Employee class as follows: 我有如下的Employee类:
Employee Class 员工类
package mypack;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name = "Employee")
public class Employee {
private String name;
private String address;
private int empId;
private String addressType;
@XmlAttribute
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getType() {
return addressType;
}
public void setType(String addressType) {
this.addressType = addressType;
}
}
I am using JAXB to marshal the object. 我正在使用JAXB封送对象。
Employee emp = new Employee();
emp.setName("ABC");
emp.setEmpId(12345);
emp.setAddress("Bangalore");
emp.setType("Residence");
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(emp, System.out);
It does not generate the required xml. 它不会生成所需的xml。 Rather it generates the xml as follows: 而是按如下所示生成xml:
**Xml Being Generated**
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Employee empId="12345">
<name>ABC</name>
<address>Bangalore</address>
<type>Residence</type>
</Employee>
Actually I don't know how to annotate the type attribute so that I will generate the xml as <address type="Resident">Bangalore</address>
Is my Employee class should be like as above? 实际上,我不知道如何注释type属性,以便将XML生成为<address type="Resident">Bangalore</address>
难道我的Employee类应该像上面一样? If yes, then how to annotate the type attribute so that it will come as an attribute in <address>
tag. 如果是,则如何注释type属性,以便它作为<address>
标记中的属性出现。
Please help me out. 请帮帮我。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB 2(JSR-222)专家组的成员。
You could use the MOXy's @XmlPath
extension for this use case: 您可以在此用例中使用MOXy的@XmlPath
扩展:
@XmlPath("address/@type")
public String getType() {
return addressType;
}
Employee 雇员
package mypack;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name = "Employee")
public class Employee {
private String name;
private String address;
private int empId;
private String addressType;
@XmlAttribute
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@XmlPath("address/@type")
public String getType() {
return addressType;
}
public void setType(String addressType) {
this.addressType = addressType;
}
}
Demo 演示
package mypack;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/mypack/input.xml");
Employee employee = (Employee) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
}
Input/Output 输入输出
<?xml version="1.0" encoding="UTF-8"?>
<Employee empId="12345">
<address type="Resident">Bangalore</address>
<name>ABC</name>
</Employee>
For More Information 欲获得更多信息
UPDATE UPDATE
If you do not want to use any vendor specific extension then you could introduce a second class to represent the address information: 如果您不想使用任何特定于供应商的扩展名,则可以引入第二类来表示地址信息:
Address 地址
package mypack;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class Address {
private String address;
private String addressType;
@XmlValue
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@XmlAttribute
public String getType() {
return addressType;
}
public void setType(String addressType) {
this.addressType = addressType;
}
}
Employee 雇员
package mypack;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Employee")
public class Employee {
private String name;
private Address address;
private int empId;
@XmlAttribute
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.