नोट: मैं EclipseLink JAXB (MOXy) नेतृत्व और JAXB 2 (JSR-222) विशेषज्ञ समूह का सदस्य हूँ।
मैं इस व्यवहार के लिए एक वृद्धि अनुरोध दर्ज किया है EclipseLink JAXB (MOXY) को जोड़ा गया:
को हल करने के
सब करता है, तो चारों ओर एक काम के रूप में अपने मैप किए गए स्ट्रिंग फ़ील्ड्स/गुणों को एक्सएमएल तत्वों में मैप किया जाता है, तो निम्न XmlAdapter दृष्टिकोण आपके लिए काम कर सकता है:
NullStringAdapter
यह XmlAdapter
String
के उदाहरण मार्शल के रूप में एक वस्तु AdaptedString
कहा जाता है। AdaptedString
में String
मूल्य और xsi:nil
विशेषता के लिए मैप किए गए फ़ील्ड शामिल हैं। XmlAdapter
में हम उस क्षेत्र का मान निर्धारित करेंगे कि String
मान शून्य है या नहीं।
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.*;
रूट
नीचे डोमेन वर्ग मैं इस उदाहरण के लिए इस्तेमाल किया गया है।यह कई स्ट्रिंग गुण है, उनमें से एक @XmlElement (nillable = true)
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>