简体   繁体   中英

need an empty XML while unmarshalling a JAXB annotated class

I have a JAXB annotated class Customer as follows

@XmlRootElement(namespace = "http://www.abc.com/customer")
public class Customer{

private String name;
private Address address;


@XmlTransient
private HashSet set = new HashSet<String>();

public String getName(){
return name;
}

@XmlElement(name = "Name", namespace = "http://www.abc.com/customer" )
public void setName(String name){
this.name = name;
set.add("name");
}

public String getAddress(){
return address;
}

@XmlElement(name = "Address", namespace = "http://www.abc.com/customer")
public void setAddress(Address address){
this.address = address;
set.add("address");
}

public HashSet getSet(){
return set;
}
}

I need to return an empty XML representing this to the user, so that he may fill the necesary values in the XML and send a request So what I require is :

<Customer>
<Name></Name>
<Address></Address>
</Customer>

If i simply create an empty object

Customer cust = new Customer() ;
marshaller.marshall(cust,sw);

all I get is the toplevel element since the other fields of the class are unset.

What can I do to get such an empty XML? I tried adding the nillable=true annotation to the elements however, this returns me an XML with the xsi:nil="true" which then causes my unmarshaller to ignore this.

How do I achieve this?

Something like <Name></Name> represents an empty, non-null String, but your java object is going to be initialized with nulls. If you want JAXB to marshall empty values, you need to set those values:

Customer cust = new Customer() ;
cust.setName("");
cust.setAddress("");
marshaller.marshall(cust, sw);

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