【问题标题】:JAXB: unmarshal diferent xml to same objectJAXB:将不同的 xml 解组到同一个对象
【发布时间】:2017-04-17 23:40:17
【问题描述】:

我有 3 个输入 XML,它们几乎具有相同的元素和属性,实际上它们代表相同的东西,所以我想将它们编组到同一个对象,如下所示:

请求一:

<?xml version="1.0" encoding="UTF-8"?>
<RequestOne>
    <id>123</id>
    <name>foo</name>
</RequestOne>

请求二:

<?xml version="1.0" encoding="UTF-8"?>
<RequestTwo>
    <id>123</id>
    <value>val</value>
</RequestTwo>

请求三:

<?xml version="1.0" encoding="UTF-8"?>
<RequestThree>
    <name>foo</name>
    <value>val</value>
</RequestThree>

所需对象(类似):

@XmlRootElement
public class Resource{

    @XmlElement
    private String id;
    @XmlElement
    private String name;
    @XmlElement
    private String value;

    //(...) more code
}

但我不能使用多个 RootElement 注释来要求 JAXB 将所有 3 个请求解组为 Resource 类的对象

有办法吗?还是我必须做 3 个单独的班级?

感谢您的帮助

【问题讨论】:

    标签: java xml jaxb unmarshalling


    【解决方案1】:

    选项 1

    使用重载的通用 unmarshal 方法解组:

    public static class Base {
        private String name ;
    
        @XmlElement(name = "name")
        public String getName() {
            return name;
        }
    
        public Base setName(String name) {
            this.name = name;
            return this;
        }
    }
    
    public static void main (String [] args) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Base.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<Base> basea = unmarshaller.unmarshal(new StreamSource(new StringReader("<RootA><name>nanana</name></RootA>")), Base.class);
        System.out.println(basea.getValue().getName());
        JAXBElement<Base> baseb = unmarshaller.unmarshal(new StreamSource(new StringReader("<RootB><name>nbnbnb</name></RootB>")), Base.class);
        System.out.println(baseb.getValue().getName());
    }
    

    选项 2

    您总是可以使用 Java 的类子类型化功能吗? JAXB 也会对父类进行注释扫描。这个例子有效

    public static class Base {
        private String name ;
    
        @XmlElement(name = "name")
        public String getName() {
            return name;
        }
    
        public Base setName(String name) {
            this.name = name;
            return this;
        }
    }
    
    @XmlRootElement( name = "RootA")
    public static class RootA extends Base{ 
    }
    
    @XmlRootElement( name = "RootB")
    public static class RootB extends Base {
    }
    
    
    public static void main (String [] args) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(RootA.class,RootB.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        RootA rootA = (RootA)unmarshaller.unmarshal(new StringReader("<RootA><name>nanana</name></RootA>"));
        System.out.println(rootA.getName());
        RootB rootB = (RootB)unmarshaller.unmarshal(new StringReader("<RootB><name>nbnbnb</name></RootB>"));
        System.out.println(rootB.getName());
    }
    

    【讨论】:

    • 非常感谢,我使用了第一个选项并且效果很好:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多