users@jaxb.java.net

[Fwd: minOccurs="0" problem]

From: Warwick Mayson <warwickm_at_forge.com.au>
Date: Thu, 13 May 2004 10:47:15 +1000

hi all

I am new to XML schemas and JAXB and am having a little trouble with the unmarshelling an XML instance. Those with a little more experience will probably see the error right away.

I am using the JAXB libs from the jwsdp-1.3

The schema is thus...

<xs:schema targetNamespace="http://www.albion.net/albion-frontend"
  xmlns:albion="http://www.albion.net/albion-frontend"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">

  <xs:element name="action-map" type="albion:action-map-type"/>

  <xs:complexType name="action-map-type">
     <xs:sequence>
        <xs:element name="servlet-directory" type="albion:servlet-directory-type"/>
        <xs:element name="default-response" type="albion:default-response-type" />
        <xs:element name="zone" type="albion:zone-type" minOccurs="0" maxOccurs="unbounded"/>
     </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="servlet-directory-type">
     <xs:restriction base="xs:string"/>
  </xs:simpleType>

  <xs:complexType name="zone-type">
     <xs:sequence>
        <xs:element name="action" type="albion:action-type" minOccurs="1" maxOccurs="unbounded"/>
     </xs:sequence>
     <xs:attribute name="id" type="xs:string" use="required"/>
  </xs:complexType>

  <xs:complexType name="action-type">
     <xs:sequence>
        <xs:element name="action-servlet" type="xs:string"/>
        <xs:element name="response" type="albion:response-type" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="default-response" type="albion:default-response-type" minOccurs="0"/>
     </xs:sequence>
     <xs:attribute name="id" type="xs:string" use="required"/>
  </xs:complexType>

  <xs:complexType name="response-type">
     <xs:simpleContent>
        <xs:extension base="xs:string">
           <xs:attribute name="id" type="xs:string" use="required"/>
        </xs:extension>
     </xs:simpleContent>
  </xs:complexType>

  <xs:complexType name="default-response-type">
     <xs:simpleContent>
        <xs:extension base="xs:string" />
     </xs:simpleContent>
  </xs:complexType>

</xs:schema>

...and an example test XML instance document

<action-map xmlns="http://www.albion.net/albion-frontend">
   <servlet-directory>myServletDir</servlet-directory>
   <default-response>myActionMapDefaultResponse</default-response>
   <zone id="zone-1">
       <action id="action-1">
           <action-servlet>myActionServlet</action-servlet>
           <default-response>myActionDefaultResponse</default-response>
       </action>
   </zone>
</action-map>

As you can see the response element defined in the action-type has a minOccurs="0" constraint. However when i attempt to unmarshell (validating) the above XML input I receive the following SAXParseException.

Could anyone explain why this is happening - i've been tearing my hair out? I would have thought that the response element is an optional child of the action node and therefore is not required in the XML instance. If the response element is present then the unmarshelling executes without complaint.

org.xml.sax.SAXParseException: Premature end of file.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:232)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:213)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:385)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:315)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(XMLScanner.java:1294)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$XMLDeclDispatcher.dispatch(XMLDocumentScannerImpl.java:670)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:348)
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:539)
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:595)
        at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1125)
        at au.net.albion.frontend.actionmap.schema.impl.runtime.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:142)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:131)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:178)
        at au.net.albion.frontend.actionmap.ActionMapMarsheller.createRequest(ActionMapMarsheller.java:147)
        at test.au.net.albion.TestOfActionMapMarsheller.testExample(TestOfActionMapMarsheller.java:66)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at junit.framework.TestCase.runTest(TestCase.java:154)
        at junit.framework.TestCase.runBare(TestCase.java:127)
        at junit.framework.TestResult$1.protect(TestResult.java:106)
        at junit.framework.TestResult.runProtected(TestResult.java:124)
        at junit.framework.TestResult.run(TestResult.java:109)
        at junit.framework.TestCase.run(TestCase.java:118)
        at junit.framework.TestSuite.runTest(TestSuite.java:208)
        at junit.framework.TestSuite.run(TestSuite.java:203)
        at junit.textui.TestRunner.doRun(TestRunner.java:116)
        at junit.textui.TestRunner.doRun(TestRunner.java:109)
        at junit.textui.TestRunner.run(TestRunner.java:72)
        at test.au.net.albion.TestOfActionMapMarsheller.main(TestOfActionMapMarsheller.java:127)
linked to ------------------
javax.xml.bind.UnmarshalException: Premature end of file.
- with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
        at au.net.albion.frontend.actionmap.schema.impl.runtime.SAXUnmarshallerHandlerImpl.handleEvent(SAXUnmarshallerHandlerImpl.java:551)
        at au.net.albion.frontend.actionmap.schema.impl.runtime.ValidatingUnmarshaller.handleEvent(ValidatingUnmarshaller.java:83)
        at au.net.albion.frontend.actionmap.schema.impl.runtime.ErrorHandlerAdaptor.propagateEvent(ErrorHandlerAdaptor.java:89)
        at au.net.albion.frontend.actionmap.schema.impl.runtime.ErrorHandlerAdaptor.fatalError(ErrorHandlerAdaptor.java:65)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:385)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:315)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(XMLScanner.java:1294)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$XMLDeclDispatcher.dispatch(XMLDocumentScannerImpl.java:670)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:348)
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:539)
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:595)
        at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1125)
        at au.net.albion.frontend.actionmap.schema.impl.runtime.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:142)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:131)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:178)
        at au.net.albion.frontend.actionmap.ActionMapMarsheller.createRequest(ActionMapMarsheller.java:147)
        at test.au.net.albion.TestOfActionMapMarsheller.testExample(TestOfActionMapMarsheller.java:66)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at test.au.net.albion.TestOfActionMapMarsheller.main(TestOfActionMapMarsheller.java:127)

Thanks in advance

Warwick Mayson




-- 
"To go forward, you must first backup." 
***********************************************************************
This message contains privileged and confidential information intended
only for the use of the addressee named above.  If you are not the
intended recipient of this message you must not disseminate, copy or
take any action in reliance on it.  If you have received this message
in error please notify the sender immediately.  Any views expressed in
this message are those of the individual sender, except where the
sender specifically states them to be the views of another (including
a Body Corporate).
************************************************************************
If you wish to opt out from future messages, send an email to
unsubscribe_at_qiqsolutions.com with the subject UNSUBSCRIBE
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net