[英]Generating attribute option in xml 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>
我必須使用JAXB生成上面的xml。 我有如下的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 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;
}
}
我正在使用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);
它不會生成所需的xml。 而是按如下所示生成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>
實際上,我不知道如何注釋type屬性,以便將XML生成為<address type="Resident">Bangalore</address>
難道我的Employee類應該像上面一樣? 如果是,則如何注釋type屬性,以便它作為<address>
標記中的屬性出現。
請幫幫我。
注意:我是EclipseLink JAXB(MOXy)的負責人,也是JAXB 2(JSR-222)專家組的成員。
您可以在此用例中使用MOXy的@XmlPath
擴展:
@XmlPath("address/@type")
public String getType() {
return addressType;
}
雇員
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;
}
}
演示
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);
}
}
輸入輸出
<?xml version="1.0" encoding="UTF-8"?>
<Employee empId="12345">
<address type="Resident">Bangalore</address>
<name>ABC</name>
</Employee>
欲獲得更多信息
UPDATE
如果您不想使用任何特定於供應商的擴展名,則可以引入第二類來表示地址信息:
地址
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;
}
}
雇員
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.