Hi again azimo wrote: > Hi there, > > I know I left out a lot but you have completely correctly understood my > question in your response. I was not exactly asking about the property but > it was good of you to comment on that as well. The behaviour you describe is > exactly what happens, but here is where my question comes in. Is there any > way to get Castor to add a NULL value even though there is no value (i.e. > the element is present but empty)?? Not to my knowledge.
> Or will this always be considered different from NULL (i.e. no element at > all)?? Yes. As already explained, a minOccurs="0" indicates that the element can be (optionally) included in your XML, but this is not mandatory. And this is an exclusive OR: either it is included or it is not. When it is included, the fact that it is included mandates that it has to meet the remainder of the criteria in your XML schema. In your case - and here I am taking the XML schema fragment from below - this implies that if there's an <SomeNiceElementName> ...</SomeNiceElementName> included in your XML, its value space is that of <xs:integer> and the value has to meet the 'totalDigits' facet as well. In other words, having <SomeNiceElementName></SomeNiceElementName> or <SomeNiceElementName/> would be invalid and violate the element definition (aka anonymous simple type definition specified). > Is there any easy way to make these two the same? Not really. There's the nillable attribute you could use in your XML schema, but that would alter the semantics as well. > I saw someone else > that had extended the Marshaller class for something similar which is > something I can do but I would prefer not to. I'd rather not have you doing this .. ;-). Any sub-classing of Marshaller that implemented your 'request' would break the basic contracts of XML and schema constraints as defined in your XML schema. > > Definiton of element: > > <xs:element name="SomeNiceElementName" minOccurs="0"> > <xs:annotation> > <xs:documentation/> > </xs:annotation> > <xs:simpleType> > <xs:restriction base="xs:integer"> > <xs:totalDigits value="5"/> > </xs:restriction> > </xs:simpleType> > </xs:element> > > Some additional info. This is an element not defined by me. The problem we > have is something I am simply dabbling with on the side, I don't know enough > of XML schemas to say if that is poorly written or not. Yes, it is, if there's a requirement that this element should be able to carry 'null' values. I hope that my comments above make things clearer to you. > I also understand > that not having the element present in the XML vs. getting an empty element > in the XML is not the same thing, I would just like to know if there is a > nice and simple way of making it the same thing (usually I have little or no > use for an empty string "" or 0 and would in many instances always want to > set NULL to an attribute that receives this). Not the same thing, but you could use the 'nillable' attribute on the element definition, and allow out-ouf-band signals about emptiness. > > Thanks for the answer, > > Archie > > > Werner Guttmann wrote: >> Hi Archie, >> >> It looks like you are talking about the following property >> >> # Selects use of Object Wrappers instead of primitives (e.g >> java.lang.Float >> # instead of float). >> # False by default. >> # >> #org.exolab.castor.builder.primitivetowrapper=false >> >> as defined in the default *castorbuilder.properties* file packaged with >> the codegen JAR. As such I wonder whether you are using a mapping file >> based approach for XML data binding, or whether you are starting with an >> XML schema and generate Java code from it. >> >> Having said that, let's look at this in a more general context. When an >> XML schema defines an element (definition) as optional, the idea is that >> it's use is optional; so it either will be present or it won't. Once it >> is present in the corresponding XML document, it is assumed as present, >> and a value will be 'deduced' during unmarshalling. If there's no value >> (which obviously does *not* match your XML schema), a default value will >> be 'injected' in the Java property that corresponds to your element >> definition. For a primitive type (int, long, ....), this means that a >> value of '0' will be set. For primitive wrappers I am not sure, but I >> think that the same will happen, as your XML document instance actually >> comes with the element being defined (though empty). >> >> What does the element definition precisely look like ? >> >> Werner >> >> azimo wrote: >>> Hi all, >>> >>> I have a schema which defines an element as optional. Sometimes this >>> element >>> is not present in the XML that I receive in which case Castor works just >>> fine and the object generated in my Java code contains NULL for the given >>> element. Sometimes however, the XML contains an empty start-end tag which >>> (since my element is numeric) generates the default value of 0. Castor >>> properties has the setting primitivestowrapper set to true so I would >>> like >>> NULL for this as well. Is there any way to make sure that Castor sees an >>> empty start-end tag the same way as if it wasn't present at all?? >>> >>> I've read the documentation and can't find it myself if there is. >>> >>> Thanks in advance, >>> >>> Archie >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> >> > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

