我需要在 jaxb 中将 null 值显示为空元素。我正在使用 jaxb 的 moxy 实现。 我找到了这个选项
@XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
是否有任何类似的扩展可以应用于类级别(对于其中定义的所有元素)
最佳答案
我强烈建议代表 null
没有节点或有 xsi:nil="true"
属性。这最适合模式验证(即 <age/>
或 <age></age>
不是 xsd:int
类型的有效元素。但是,如果您不能,这里是如何完成您的用例:
标准 JAXB 行为
使用标准 API,您可以控制 null 是表示为不存在的节点还是用 xsi:nil="true"
表示与 @XmlElement
注释(参见:http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html)。
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
private String street;
@XmlElement(nillable=true)
private String city;
}
如果两个字段的值都为空,则以下是 XML 输出。
<?xml version="1.0" encoding="UTF-8"?>
<address>
<city xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</address>
MOXy - 覆盖每个类的此行为
MOXy 不提供注释来为类的所有属性指定空策略。但是,您可以利用 DescriptorCustomizer
通过 @XmlCustomizer
注释并调整 native MOXy 映射元数据以完成相同的事情。
DescriptorCustomizer (AddressCustomizer)
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
public class AddressCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
for(DatabaseMapping mapping : descriptor.getMappings()) {
if(mapping.isAbstractDirectMapping()) {
XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
}
}
}
}
DomainModel(地址)
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlCustomizer(AddressCustomizer.class)
public class Address {
private String street;
@XmlElement(nillable=true)
private String city;
}
输出
<?xml version="1.0" encoding="UTF-8"?>
<address>
<street/>
<city/>
</address>
MOXy - 覆盖所有类的此行为
如果您想覆盖所有映射类的空值处理,我建议使用 SessionEventListener
反而。如果您愿意,还可以使用这种方法来更新单个类的元数据。
session 事件监听器(NullPolicySessionEventListener)
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
import org.eclipse.persistence.sessions.*;
public class NullPolicySessionEventListener extends SessionEventAdapter {
@Override
public void preLogin(SessionEvent event) {
Project project = event.getSession().getProject();
for(ClassDescriptor descriptor : project.getOrderedDescriptors()) {
for(DatabaseMapping mapping : descriptor.getMappings()) {
if(mapping.isAbstractDirectMapping()) {
XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
}
}
}
}
}
演示代码
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.sessions.SessionEventListener;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
SessionEventListener sessionEventListener = new NullPolicySessionEventListener();
properties.put(JAXBContextProperties.SESSION_EVENT_LISTENER, sessionEventListener);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Address.class}, properties);
Address address = new Address();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(address, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8"?>
<address>
<street/>
<city/>
</address>
关于java - 在 xml jaxb 中将 null 值表示为空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17366054/