Psy Jia
Psy Jia

Reputation: 81

how to set some properties not xml element

In Java , I add @XmlRootElement(name = "MyClass") to my class , it let all properties in the class become xml elements , now I have one property ,it doesn't need to be xml element , what can I do ? Thank you. George

@XmlRootElement(name = "MyClass")
public class MyClass{
    public String A ;
    public String B ;

    //what xml anotation to be set here ?
    public String notXmlelement ; 

}

Upvotes: 3

Views: 381

Answers (2)

bdoughan
bdoughan

Reputation: 149047

Excluding Less than Half of Properties

If you are excluding less than half of the mapped fields/properties then you can do this using @XmlTransient.

public Class {

    public String a;   // include
    public String b;   // include
    public String c;   // include
    @XmlTransient public String d;   // exclude

}

Excluding More than Half of Properties

If you are excluding more than half of the mapped fields/properties then you can specify @XmlAccessorType(XmlAccessType.NONE) and then only annotated fields/properties will be included.

@XmlAccessorType(XmlAccessType.NONE)
public Class {

    public String a;   // exclude
    public String b;   // exclude
    public String c;   // exclude
    @XmlElement public String d;   // include

}

For More Information

Upvotes: 3

helderdarocha
helderdarocha

Reputation: 23627

If you have a field which should not be persisted as XML (I assume you are using JAXB), then mark it as @XmlTransient.

@XmlTransient
public String notXmlelement; 

Upvotes: 2

Related Questions