2013-06-28 145 views
10

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

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

是否有任何类似的延伸,可以在类级应用(用于在其中所定义的所有元素)

回答

16

我会强烈建议表示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 outpu。 t如果两个字段的值都为空。

<?xml version="1.0" encoding="UTF-8"?> 
<address> 
    <city xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
</address> 

莫西 - 重写此行为每班

莫西不提供注释来指定一个类的所有属性空政策。但是,您可以通过@XmlCustomizer批注利用DescriptorCustomizer并调整本地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> 

莫西 - 覆盖这种行为的所有类

相反,如果要覆盖空处理所有的映射类我建议使用SessionEventListener代替。如果您愿意,也可以使用此方法更新单个课程的元数据。

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> 
0

A“坏习惯”的解决办法,如果你有在类唯一字符串字段是重写了这样的元素的setter:

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()! 
    } 
    } 
} 

与其他类型,如日期或数字。这不会工作!但有时String很足够。

@Blaise Doughan的答案似乎长期好多了,如果你能处理它。 :)