使用JAXB在xml中生成属性选项

时间:2011-10-20 12:35:54

标签: java jaxb marshalling unmarshalling jaxb2

我必须使用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> 我的员工类应该如上所述吗?如果是,那么如何注释type属性,使其成为<address>标记中的属性。

请帮帮我。

1 个答案:

答案 0 :(得分:3)

注意:我是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>

了解更多信息


<强>更新

如果您不想使用任何特定于供应商的扩展,那么您可以引入第二个类来表示地址信息:

<强>地址

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;
    }

}