참고 :은 내가 EclipseLink JAXB (MOXy) 리드와 JAXB 2 (JSR-222) 전문가 그룹의 구성원입니다.
내가이 동작을 할 수있는 개선 요청을 입력 한 EclipseLink가 JAXB (목시)에 추가 : 모든 경우 주위 작품으로
를 해결
당신의 매핑 된 문자열 필드/속성이 XML 요소에 매핑되면 다음 XmlAdapter 접근 방식을 사용할 수 있습니다.
객체가 AdaptedString
전화로이 XmlAdapter
이 String
의 인스턴스를 마샬링합니다
NullStringAdapter. AdaptedString
은 String
값과 xsi:nil
속성에 매핑 된 필드를 포함합니다. XmlAdapter
에서는 String
값이 null인지 여부에 따라 해당 필드의 값을 설정합니다.
package forum8841221;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
public class NullStringAdapter extends XmlAdapter<NullStringAdapter.AdaptedString, String> {
@Override
public AdaptedString marshal(String v) throws Exception {
AdaptedString adaptedString = new AdaptedString();
if(null == v) {
adaptedString.nil = true;
}
adaptedString.value = v;
return adaptedString;
}
@Override
public String unmarshal(AdaptedString v) throws Exception {
return v.value;
}
public static class AdaptedString {
@XmlAttribute(namespace="http://www.w3.org/2001/XMLSchema-instance")
public Boolean nil;
@XmlValue
@XmlJavaTypeAdapter(VoidStringAdapter.class)
public String value;
}
public static class VoidStringAdapter extends XmlAdapter<String, String> {
@Override
public String marshal(String v) throws Exception {
return v;
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
}
}
패키지 정보
우리는 우리가이 XmlAdapter
패키지 수준에서 XmlAdapter
를 등록하여이 패키지에있는 모든 매핑 String
필드/속성에 적용 할 것을 등록 할 수 있습니다.
@XmlJavaTypeAdapter(value=NullStringAdapter.class, type=String.class)
@XmlSchema(xmlns={@XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi")})
package forum8841221;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.bind.annotation.*;
루트 아래
내가이, 예를 들면 사용한 도메인 클래스입니다.그것은 그들 중 하나 (nillable로 = TRUE) @XmlElement
package forum8841221;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private String a;
private String b;
private String c;
private String d;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
@XmlElement(nillable=true)
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
}
데모
package forum8841221;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.setB("B");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
출력의 주석을 붙일 수 있고, 여러 가지 문자열 속성이 있습니다
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a xsi:nil="true"/>
<b>B</b>
<c xsi:nil="true"/>
<d xsi:nil="true"/>
</root>