偏见声明- 我是EclipseLink JAXB (MOXy)负责人
使用EclipseLink JAXB (MOXy),您可能可以获得所需的格式。这将允许您消除 JDOM 部分以减少内存占用。
例子
假设我们的模型中有以下类(为了节省空间而省略了访问器):
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
private Address billingAddress;
private Address shippingAddress;
}
JAXB 映射
由于EclipseLink MOXy符合 JAXB 规范 ( JSR-222 ),因此默认情况下会生成以下文档:
<customer>
<billingAddress/>
<shippingAddress/>
</customer>
路径映射
如果您需要对 XML 进行细粒度控制,您可以使用 MOXy 的 @XmlPath 扩展。注释字段如下:
@XmlPath("contact-info/billing-address")
private Address billingAddress;
@XmlPath("contact-info/shipping-address")
private Address shippingAddress;
将导致生成以下 XML:
<customer>
<contact-info>
<billing-address/>
<shipping-address/>
</contact-info>
</customer>
位置映射
XPath 片段可以包含一个位置指示符:
@XmlPath("address[1]")
private Address billingAddress;
@XmlPath("address[2]")
private Address shippingAddress;
生成的 XML 将如下所示:
<customer>
<address/>
<address/>
</customer>
条件映射
@XmlPath("address[@type='billing']")
private Address billingAddress;
@XmlPath("address[@type='shipping']")
private Address shippingAddress;
生成的 XML 将是:
<customer>
<address type="billing"/>
<address type="shipping"/>
</customer>
了解更多信息