2012-12-11 8 views
8

को देखते हुए निम्न उदाहरण XSD स्निपेट:JAXB xjc: स्ट्रिंग्स के लिए कोड कैसे उत्पन्न करें जो मान शून्य होने पर खाली हो जाता है?

< xs:attribute name="SEGMENT" default="" use="optional" type="xs:string"/ > 

जब xjc वर्ग SEGMENT सेम विशेषता युक्त उत्पन्न करता है, निम्नलिखित गेटर है स्वत: जनरेट:

public String getSEGMENT() { 
    if (segment == null) { 
     return ""; 
    } else { 
     return segment; 
    } 
} 

मेरा प्रश्न है कि आप इसे कैसे मिलता है xs:element ऑब्जेक्ट्स के लिए ऐसा ही करें? दूसरे शब्दों में, यह देखते हुए निम्नलिखित XSD स्निपेट:

public String getNAME() { 
    if (name == null) { 
     return ""; 
    } else { 
     return name; 
    } 
} 

यह कैसे किया जा सकता है:

< xs:element name="NAME" default="" type="xs:string"/ > 

मैं अगर मैं xjc मिल निम्नलिखित उत्पन्न करने के लिए कर सकते हैं जानना चाहते हैं? के रूप में यह डिफ़ॉल्ट मान के साथ एक विशेषता के लिए करता है

उत्तर

2

JAXB डिफ़ॉल्ट मान के साथ एक तत्व के लिए एक ही कोड उत्पन्न नहीं करता है क्योंकि XML schema differentiates between element and attribute defaults:

Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.

The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

तुम हमेशा एक लापता विशेषता के लिए डिफ़ॉल्ट मान पर भरोसा कर सकते (यहां से विशेष गेटर) लेकिन गायब तत्व मान के साथ एक पकड़ है।

फिर भी, जब आप एक उदाहरण को अनमशॉल करते हैं, तो अनमशैलर जानता है कि डिफ़ॉल्ट मान को कैसे संभाला जाए। विवरण के लिए यहां देखें:

XJC गेटर कोड डिफ़ॉल्ट मान के साथ, जोड़ सकते हैं या प्रारंभ नहीं होगा क्षेत्रों ताकि आप "शून्य सुरक्षित जांच" की जरूरत है आप अगर यह अपने आप मैन्युअल जोड़ने या तो कर सकते हैं के बाद कोड XJC द्वारा उत्पन्न होता है या कुछ प्लगइन का उपयोग करने के लिए यह स्वचालित रूप से करने की कोशिश:

+0

एक क्षेत्र प्रारंभकर्ता निश्चित रूप से उपयुक्त होगा। अब सवाल यह है कि मैं xjc को प्रत्येक वर्ग में प्रत्येक स्ट्रिंग में खाली स्ट्रिंग प्रारंभकर्ता जोड़ने के लिए कैसे प्राप्त करूं? शायद एक अलग बाइंडिंग फ़ाइल में? –

+0

@ जावा लावा: मैंने अपने उत्तर में कुछ और विवरण जोड़े और कुछ प्लगइन्स भी जो दिलचस्प लगते हैं। देखें कि यह मदद करता है। – Bogdan

+0

अतिरिक्त जानकारी के लिए धन्यवाद। मुझे हालांकि एक समस्या है। आपके द्वारा भेजे गए लिंक में एक मेवेन या चींटी कार्य स्थापित करना शामिल है। मैं मेवेन का उपयोग नहीं करता। और चींटी कार्य को स्थापित करते समय समस्या यह है कि मुझे एक लिंकेज त्रुटि मिल रही है जिसमें कहा गया है कि अलग-अलग वर्गों को उसी नाम से लोड किया जा रहा है। समस्या, मुझे विश्वास है, जावा 1.6 में xjc बनाया गया है, और jxb-xjc-2.1.9.jar को मेरे xjc taskdef classpath में जोड़कर, वे संघर्ष कर रहे हैं। मेरा सवाल यह है कि 1.6 को अनदेखा करने के लिए या तो एंटी स्क्रिप्ट कैसे प्राप्त कर सकता है (मेरा JAVA_HOME उस पर सेट है) और मेरे taskdef classpath का उपयोग करें, या taskdef में 1.6 xjc jar का संदर्भ लें? –

संबंधित मुद्दे