JAXB: How do I annotate classes so that they belong to different namespaces?

后端 未结 1 701
春和景丽
春和景丽 2021-02-08 14:01

I want to have JAXB-annotated classes which would be marshalled/unmarshalled to different XML namespaces.

What I need is something like

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 14:48

    @XmlRootElement(name="someRootElement", namespace = "urn:my:ns1")
    class Test {
        @XmlElement(name="someElement", namespace="urn:my:ns1")
        String elem1 = "One";
    
        @XmlElement(name="someElement", namespace="urn:my:ns2")
        String elem2 = "Two";
    
        @XmlElement(name="someElement", namespace="urn:my:ns3")
        String elem3 = "Three";
    }
    

    This marshals into the following XML:

    
    
        One
        Two
        Three
    
    

    If you are using JAXB RI and don't like the default ns2 and ns3 namespace prefixes, you need to provide your own NamespacePrefixMapper.

    0 讨论(0)
提交回复
热议问题