JAXB和动态选择类来实例化

时间:2017-09-05 07:14:15

标签: java jaxb unmarshalling jaxb2

我有一个配置的xml表示如下。

<definitions>
    <definition type="MessageReception"> ... </definition>
    <definition type="MessageProcessing"> ... </definition>
    <definition type="ResponseGeneration"> ... </definition>
</definition>

如您所见,定义的类型取决于属性&#34; type&#34;。 我想使用JAXB框架来解组它。但我只找到了JAXB用法的例子,非常基本的情况,比如书籍,标题,作者,年份......

有没有一种简单的方法可以做我想做的事情?

2 个答案:

答案 0 :(得分:0)

为“定义”创建内部类时,应标记注释为@XmlAttribute的“type”成员。

以下是给定xml的基本工作演示;

public class UnmarshalJaxbDemo {


    public static void main(String[] args) {
        StringBuffer xmlStr = new StringBuffer( "<definitions>"+
                                    "<definition type=\"MessageReception\"> ... </definition>"+
                                    "<definition type=\"MessageProcessing\"> ... </definition>"+
                                    "<definition type=\"ResponseGeneration\"> ... </definition>"+
                                     "</definitions>" );
        try {
            JAXBContext context = JAXBContext.newInstance(Definitions.class);
            Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
            Definitions definitions = (Definitions) jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(xmlStr.toString())));

            for (Definition defitinion : definitions.getDefinition()) {
                System.out.println(defitinion.getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Definition {

        @XmlAttribute
        private String type;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

    }

    @XmlRootElement(name = "definitions")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Definitions {
        private List<Definition> definition;

        public List<Definition> getDefinition() {
            return definition;
        }

        public void setDefinition(List<Definition> definition) {
            this.definition = definition;
        }

    }

}

答案 1 :(得分:0)

您可以使用xsi:type来指示类上的jaxb进行实例化。 例如:

<definitions>
   <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="messageReception">
      <receptionField>foo</receptionField>
   </definition>
   <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="messageProcessing">
      <processingField>bar</processingField>
   </definition>
   <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="responseGeneration">
      <generationField>baz</generationField>
   </definition>
</definitions>

package your.package

class MessageReception {
   // getters and setters omitted
   String receptionField;
}

jaxbContext = JAXBContext.newInstance("your.package");
Unmarshaller unmarshaller = mJaxbContext.createUnmarshaller();
DefinitionList definitionList = (DefinitionList) unmarshaller.unmarshal(inputStream);
相关问题