Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Make minOccurs 1, to make month required;</p> </blockquote> <p>The default value for <code>minOccurs</code> is one, so the <code>month</code> element is required. Note how <code>minOccurs="0"</code> had to be added to <code>userLogin</code> to make it optional.</p> <pre><code>&lt;xs:complexType name="wsData"&gt; &lt;xs:sequence&gt; &lt;xs:element name="month" type="xs:string"/&gt; &lt;xs:element minOccurs="0" name="userLogin" type="xs:string"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; </code></pre> <blockquote> <p>Validate month with its generated restriction (without a XmlAdapter).</p> </blockquote> <p>You can set an instance of <code>Schema</code> on the Unmarshaller to have the input validated:</p> <ul> <li><a href="http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html" rel="noreferrer">http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html</a></li> </ul> <p><strong>Demo</strong></p> <p>The following code can be used to generate the XML schema:</p> <pre><code>package forum9111936; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.SchemaOutputResolver; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(WSData.class); SchemaOutputResolver sor = new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { StreamResult result = new StreamResult(System.out); result.setSystemId(suggestedFileName); return result; } }; jc.generateSchema(sor); System.out.println(); } } </code></pre> <hr/> <p><strong>UPDATE</strong></p> <p>The JAXB RI normally throws a <code>ValidationEvent</code> of severity 1 for conversion issues. The default <code>ValidationEventHandler</code> ignores all issues of severity less than 2. This normally results in the value being set to null. You can override the <code>ValidationEventHandler</code> as follows:</p> <pre><code> unmarshaller.setEventHandler(new ValidationEventHandler() { @Override public boolean handleEvent(ValidationEvent event) { System.out.println(event); return event.getSeverity() &lt; ValidationEvent.ERROR; } }); </code></pre> <p>However the JAXB RI does not appear to throw events related to converting enum values (possible bug). If you happen to be using EclipseLink JAXB (MOXy) as your JAXB provider then you will get an exception like:</p> <pre><code>Exception in thread "main" Local Exception Stack: Exception [EclipseLink-116] (Eclipse Persistence Services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.DescriptorException Exception Description: No conversion value provided for the value [13] in field [month/text()]. Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[month--&gt;month/text()] Descriptor: XMLDescriptor(forum9111936.WSData --&gt; []) at org.eclipse.persistence.exceptions.DescriptorException.noFieldValueConversionToAttributeValueProvided(DescriptorException.java:1052) at org.eclipse.persistence.mappings.converters.ObjectTypeConverter.convertDataValueToObjectValue(ObjectTypeConverter.java:140) at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:287) at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:190) at org.eclipse.persistence.oxm.record.UnmarshalRecord.endElement(UnmarshalRecord.java:910) at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:133) at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:83) at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:72) at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:838) at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:626) at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:472) at forum9111936.Demo2.main(Demo2.java:30) </code></pre> <p><strong>For More Information</strong></p> <ul> <li><a href="http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html" rel="noreferrer">http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload