Convert Polymorphic(多态) Object to XML using JAX-B

本文介绍如何使用JAXB将包含多态的Java对象转换为XML格式,并提供了一个具体的例子,展示了继承自同一父类的不同子类对象如何被正确地序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When you use polymorphism in your classes and you want to convert your object to XML using JAX-B, here is an example how to.

Abstract Animal Class
package com.sheting.basic.xml.jaxb;

import javax.xml.bind.annotation.XmlSeeAlso;

@XmlSeeAlso({ Dog.class, Cat.class })
public class Animal {

}
Cat Inherits from Animal
package com.sheting.basic.xml.jaxb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "cat")
public class Cat extends Animal {

    private String catPro;

    public String getCatPro() {
        return catPro;
    }

    public void setCatPro(String catPro) {
        this.catPro = catPro;
    }

}
Dog Inherits from Animal
package com.sheting.basic.xml.jaxb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "dog")
public class Dog extends Animal {

    private String dogPro;

    public String getDogPro() {
        return dogPro;
    }

    public void setDogPro(String dogPro) {
        this.dogPro = dogPro;
    }

}
Root entity class for converting list of animals
package com.sheting.basic.xml.jaxb;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "root")
public class Root {

    private List<Animal> animals;

    public List<Animal> getAnimals() {
        return animals;
    }

    @XmlElementWrapper(name = "animals")
    @XmlElements({ @XmlElement(name = "dog", type = Dog.class), @XmlElement(name = "cat", type = Cat.class) })
    public void setAnimals(List<Animal> animals) {
        this.animals = animals;
    }

}
Convert Polymorphic Object to XML
package com.sheting.basic.xml.jaxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class PolymorphismObjectXml {
    public static void main(String... args) throws JAXBException {
        // create object
        List<Animal> animals = new ArrayList<Animal>();
        animals.add(new Dog());
        animals.add(new Cat());

        Root root = new Root();
        root.setAnimals(animals);

        // create jaxb context
        JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);

        // create jaxb marshaller
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // pretty print xml
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        // marshal object
        jaxbMarshaller.marshal(root, System.out);
    }
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <animals>
        <dog/>
        <cat/>
    </animals>
</root>

question: 为什么子类的属性没有显示? 有时间再研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值