关于网站建设的实训报告,上海阳性增多,手机排行榜软件,重庆低价网站建设我强烈建议使用不存在节点或xsi#xff1a;nil “true”属性来表示null.这最适用于模式验证(即 age /或 age / age不是xsd#xff1a;int类型的有效元素.但是,如果您不能在这里完成您的用例#xff1a;标准JAXB行为import javax.xml.bind.annotatio…我强烈建议使用不存在节点或xsinil “true”属性来表示null.这最适用于模式验证(即 age /或 age / age不是xsdint类型的有效元素.但是,如果您不能在这里完成您的用例标准JAXB行为import javax.xml.bind.annotation.*;XmlRootElementXmlAccessorType(XmlAccessType.FIELD)public class Address {private String street;XmlElement(nillabletrue)private String city;}以下是两个字段的值为空的XML输出.MOXY – 覆盖此类行为MOXy不提供注释来为类中的所有属性指定空策略.但是,您可以通过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 {Overridepublic 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;XmlRootElementXmlAccessorType(XmlAccessType.FIELD)XmlCustomizer(AddressCustomizer.class)public class Address {private String street;XmlElement(nillabletrue)private String city;}产量MOXY – 覆盖所有类别的行为如果你想覆盖所有映射类的空处理,我建议改用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 {Overridepublic 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 properties new HashMap(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);}}产量