繁体   English   中英

在解组JAXB带注释的类时需要空XML

[英]need an empty XML while unmarshalling a JAXB annotated class

我有一个JAXB注释的类Customer,如下所示

@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;
}
}

我需要向用户返回一个表示此内容的空XML,以便他可以填写XML中的必要值并发送请求,所以我需要的是:

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

如果我只是创建一个空对象

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

我得到的只是顶层元素,因为该类的其他字段未设置。

我怎样做才能得到这样一个空的XML? 我尝试将nillable = true批注添加到元素,但是,这返回了一个带有xsi:nil =“ true”的XML,这随后使我的编组者忽略了这一点。

我该如何实现?

<Name></Name>这样的东西表示一个空的非null字符串,但是您的java对象将使用null进行初始化。 如果希望JAXB将空值编组,则需要设置这些值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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