14

我需要在 jaxb 中将空值显示为空元素。我正在使用 jaxb 的 moxy 实现。我找到了这个选项

@XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)

是否有任何类似的扩展可以应用于类级别(对于其中定义的所有元素)

4

2 回答 2

20

我强烈建议null使用不存在节点或使用xsi:nil="true"属性来表示。这最适用于模式验证(即<age/>,或者<age></age>不是 type 的有效元素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 不提供注释来为类上的所有属性指定 null 策略。但是,您可以DescriptorCustomizer通过@XmlCustomizer注释利用 a 并调整本机 MOXy 映射元数据来完成同样的事情。

描述符定制器(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);
            }
        }
    }

}

域模型(地址)

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 - 覆盖所有类的这种行为

相反,如果您想覆盖所有映射类的 null 处理,我建议您改用 a SessionEventListener。如果您愿意,也可以使用这种方法来更新单个类的元数据。

会话事件监听器 (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>
于 2013-06-28T14:08:30.157 回答
0

如果您在类中只有 String 字段,则“不好的做法”解决方法是覆盖元素的设置器,如下所示:

public class Address {

  private String street;

  @XmlElement(name = "street")
  public void setStreet(String street){
    this.street = street;
    if (this.street == null){
      this.street = ""; // Not NULL, empty string like new String()!
    }
  }
}

这不适用于日期或数字等其他类型!但有时字符串就足够了。

从长远来看,@Blaise Doughan 的答案似乎要好得多,如果你能应付的话。:)

于 2016-08-15T13:27:57.983 回答