笔记
Jackson 不是符合JAXB (JSR-222) 的实现。这意味着无法保证它如何解释标准 JAXB 注释
默认情况下,符合JAXB (JSR-222) 的实现不会将包装器元素应用于集合属性。
一个
默认情况下,JAXB (JSR-222) 实现将基于属性的默认映射。为了节省空间,我省略了这些方法并指定@XmlAccessorType(XmlAccessType.FIELD)
了元数据将从实例变量(字段)派生。
package forum13097559;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
private String b1;
private String b2;
private List<B3> b3;
}
B3
package forum13097559;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class B3 {
private String c1;
private String c2;
}
演示
package forum13097559;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(A.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13097559/input.xml");
A a = (A) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(a, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b1>b1</b1>
<b2>b2</b2>
<b3>
<c1></c1>
<c2></c2>
</b3>
<b3>
<c1></c1>
<c2></c2>
</b3>
<b3>
<c1></c1>
<c2></c2>
</b3>
</a>
了解更多信息