使用 JAXB 在 xml 中生成属性选项
我必须使用 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 类如下:
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;
}
}
我正在使用 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 生成为 Bangalore
我的 Employee 类应该像上面那样吗?如果是,那么如何注释 type 属性,以便它将作为
标记中的属性。请帮帮我。
I have to generate the following xml in my program using JAXB.
<?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.
I am having the Employee class as follows:
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.
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. Rather it generates the xml as follows:
**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? If yes, then how to annotate the type attribute so that it will come as an attribute in <address>
tag.
Please help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。
对于此用例,您可以使用 MOXy 的
@XmlPath
扩展:Employee
Demo
Input/Output
For更多信息
更新
如果您不想使用任何供应商特定的扩展,那么您可以引入第二类来表示地址信息:
地址
员工
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
You could use the MOXy's
@XmlPath
extension for this use case:Employee
Demo
Input/Output
For More Information
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
Employee