need an empty XML while unmarshalling a JAXB annotated class

Posted by sswdeveloper on Stack Overflow See other posts from Stack Overflow or by sswdeveloper
Published on 2010-04-22T10:45:08Z Indexed on 2010/04/22 10:53 UTC
Read the original article Hit count: 218

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about jaxb