简体   繁体   中英

Single Element Name mapping for all instances of a java class using jaxb

I have a class named A which is used in some other classes like B,C in different variable names ,

@XmlRootElement(name="global")
public class A{    
}

@XmlRootElement
public class B{
    @XmlElement
    private A first;
}

@XmlRootElement
public class C{
    @XmlElement
    private A second;
}

Now when I marshall class BI got the A element like

<b:first>...........</b:first>

And when I marshall class CI got the A element like

<c:second>...........</c:second>

But I want to get the name "glabal" for all the occurences of class A in whichever class it is . I know i can use @XmlElement(name="global") in B and C . But i don't want to use any parameter like name,namespaces in @XmlElement field . How can I annotate class A so that whenever any other class declares an instance of A , every time it will marshaled as <b:global></b:global> or <c:global><c:global>

If the target class is annotated with @XmlRootElement then instead of @XmlElement you can use @XmlElementRef . In this way you can define the element information once. Since the element info is defined once it will have one namespace URI associated with it. If you want the namespace info to be different then you will need to use @XmlElement .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM