use*_*013 1 null attributes jaxb marshalling
marshaller是否有办法生成一个跳过任何null属性的新xml文件?因此someAttribute =""之类的内容不会出现在文件中.
谢谢
甲JAXB(JSR-222)与实现不会编组注释字段/属性@XmlAttribute包含空值.
Java模型(根)
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Root {
private String foo;
private String bar;
@XmlAttribute
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
@XmlAttribute(required=true)
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud)
演示代码
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.setFoo(null);
root.setBar(null);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
<?xml version="1.0" encoding="UTF-8"?>
<root/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9610 次 |
| 最近记录: |