2013-04-30 59 views
1

我正在使用Jaxb2Marshaller並使用ValidationEventHandler-handleEvent()。我在映射過程中存儲發生的任何錯誤消息。我使用xsd來驗證強制/可選標記。所以這個問題取決於xml中一個標籤的值,其他一些標籤的值可能成爲強制/可選的。如何通過xsd在java中驗證xml取決於xml元素的值

現在對於所有的情況,我已經做了單獨的xsd,嘗試檢查各自的必填字段。但我怎麼能告訴jaxb,以驗證應該使用xsd的xml?

<bean id="jaxb2MarshallerForABC" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="contextPath"> 
      <util:constant 
       static-field="com.abc.pqr.util.ABCMessageConstants.PACKAGE_DATAMODEL_AB_IN_OB" /> 
     </property> 
     <property name="schemas"> 
      <value> 
       classpath:xsd/tc/oa/in/InboundMessageIss.xsd <!--Depending on xml value validate with different xsd/since there could be diffent tag as mandatory/optional --> 
      </value> 
     </property> 
     <property name="unmarshallerListener" ref="unMarshallingListenerForOA"/> 
     <property name="validationEventHandler" ref="validationEventHandler" /> 
    </bean> 

回答

0

也許這樣的事情?:

<bean id="contractUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>Class1</value> 
      <value>Class2</value> 
     </list> 
    </property> 
    <property name="schemas"> 
     <list> 
      <value>Class1Schema</value> 
      <value>Class2Schema</value> 
     </list> 
    </property> 
</bean>