Java 如何使用@XmlElement 和@XmlRootElement 在对象内编组对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23601681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:15:19  来源:igfitidea点击:

How to use @XmlElement and @XmlRootElement for marshalling object inside an object?

javaxmljaxb

提问by Akshay Lokur

I have seen many implementations of JAXB where we can convert java primitive to XML element using @XmlElementannotation.

我见过很多 JAXB 的实现,我们可以使用@XmlElement注释将 java 原语转换为 XML 元素。

But, I want to convert following POJO to XML (Note there is an address objectinside employee class and not just primitives):

但是,我想将以下 POJO 转换为 XML(请注意,员工类中有一个地址对象,而不仅仅是基元):

public class Employee {
    private Address address;
    private int employeeId;

    // constructors + setters + getters
}

How to use these JAXB annotations to marshall an employee object to XML?

如何使用这些 JAXB 注释将员工对象编组为 XML?

Thanks.

谢谢。

采纳答案by Akshay Lokur

I was able to achieve "object inside object" XML marshalling with JAXB by following appraoch given below (i.e. by annotating both the classes with @XmlRootElementannotation):

通过遵循下面给出的方法(即通过注释对两个类进行@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 {
    .
    .
}

回答by Hirak

Using Jaxb you can try the following code. Alternatively you may try Xstream

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

}

回答by bdoughan

There is nothing different you need to do to marshal a POJO property from what you do to marshal a primitive property. The referenced POJO class does not need to be annotated with @XmlRootElement.

编组 POJO 属性所需的操作与编组原始属性所需的操作没有什么不同。引用的 POJO 类不需要用@XmlRootElement.

Java Model

Java模型

Employee

员工

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

地址

There is nothing special that you need to do to have Addressmarshalled as part of Employee.

您无需执行任何特殊操作即可将Address编组作为Employee.

public class Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

Demo Code

演示代码

Below is some demo code that will populate and employee model and marshal it to XML.

下面是一些演示代码,这些代码将填充员工模型并将其编组为 XML。

Demo

演示

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

}

Output

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <address>
        <street>1 A Street</street>
    </address>
    <employeeId>123</employeeId>
</employee>


Renaming the Marshalled Element

重命名编组元素

If you want to override the default element name then you can use the @XmlElementannotation regardless of what type the property is.

如果要覆盖默认元素名称,则@XmlElement无论属性是什么类型,都可以使用注释。

Employee

员工

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

}

Output

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <ADDR>
        <street>1 A Street</street>
    </ADDR>
    <ID>123</ID>
</employee>