支持不同类型的对象的JaxB表示

支持不同类型的对象的JaxB表示,jaxb,Jaxb,以下java属性可以是字符串/整数类型。我尝试添加注释,如下所示,但它只接受最后一种类型(本例中为字符串),即使传递了整数也是如此。如何允许此属性使用多种类型 @XmlElements({ @XmlElement(type = Integer.class), @XmlElement(type = String.class) }) private Object val1; 问题是在解组时没有名称来区分不同类型的类。我不知道您的具体要求是什么,但您可以通过以下方式实现: @XmlRootEleme

以下java属性可以是字符串/整数类型。我尝试添加注释,如下所示,但它只接受最后一种类型(本例中为字符串),即使传递了整数也是如此。如何允许此属性使用多种类型

@XmlElements({
@XmlElement(type = Integer.class), @XmlElement(type = String.class)
})
private Object val1;

问题是在解组时没有名称来区分不同类型的类。我不知道您的具体要求是什么,但您可以通过以下方式实现:

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class SomeRoot {
    @XmlElements({
            @XmlElement(name="string", type = String .class),
            @XmlElement(name="int", type = Integer.class)
    })
    private Object val;

    public SomeRoot(Object val) {
        this.val = val;
    }

    public SomeRoot() { }
}
当您提供了字符串值时,它将被封送为如下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <string>test</string>
</root>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <int>1</int>
</root>
在简单的main中尝试此操作,将提供的xml解析为val上字符串和整数的正确类型:

public static void main(String[] args) {
        try {
            File file = new File("file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(SomeRoot.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            jaxbUnmarshaller.setAdapter(new MyXmlAdapter());

            SomeRoot pojo = (SomeRoot) jaxbUnmarshaller.unmarshal(file);
            System.out.println(pojo.getVal().getClass());
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

问题是在解组时没有名称来区分不同类型的类。我不知道您的具体要求是什么,但您可以通过以下方式实现:

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class SomeRoot {
    @XmlElements({
            @XmlElement(name="string", type = String .class),
            @XmlElement(name="int", type = Integer.class)
    })
    private Object val;

    public SomeRoot(Object val) {
        this.val = val;
    }

    public SomeRoot() { }
}
当您提供了字符串值时,它将被封送为如下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <string>test</string>
</root>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <int>1</int>
</root>
在简单的main中尝试此操作,将提供的xml解析为val上字符串和整数的正确类型:

public static void main(String[] args) {
        try {
            File file = new File("file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(SomeRoot.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            jaxbUnmarshaller.setAdapter(new MyXmlAdapter());

            SomeRoot pojo = (SomeRoot) jaxbUnmarshaller.unmarshal(file);
            System.out.println(pojo.getVal().getClass());
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

谢谢@mart,但问题是此值将仅通过一个属性发送。一个名字不能有多种类型吗?示例xml内容如下所示,其中可以有string/int.integer 1,谢谢@mart,但问题是此值将仅通过一个属性发送。一个名字不能有多种类型吗?示例xml内容如下所示,其中可以有string/int.integer 1