2012-10-16 3 views
7

il seguente file bindings JAXB crea le classi adattatore come previsto, ma Eclipse e XMLSpy dire che è non valida:JAXB associazioni di file: convalida errore

<?xml version="1.0" encoding="UTF-8"?> 
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1"> 


     <jxb:globalBindings> 
      <jxb:javaType name="java.util.Calendar" xmlType="xs:date" parseMethod="javax.xml.bind.DatatypeConverter.parseDate" 
       printMethod="javax.xml.bind.DatatypeConverter.printDate" /> 
      <jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" 
       printMethod="javax.xml.bind.DatatypeConverter.printDateTime" /> 
      <jxb:javaType name="java.util.Calendar" xmlType="xs:time" parseMethod="javax.xml.bind.DatatypeConverter.parseTime" 
       printMethod="javax.xml.bind.DatatypeConverter.printTime" /> 
     </jxb:globalBindings> 

</jxb:bindings> 

L'errore è qualcosa di simile:

cvc-complex-type.2.4.b: The content of element 'jxb:globalBindings' is not complete. One of '{"http://java.sun.com/xml/ns/jaxb":javaType, "http://java.sun.com/xml/ns/jaxb":serializable, WC[##other:"http://java.sun.com/xml/ns/jaxb"]}' is expected. 

Si noti che il file dello schema dei collegamenti JAXB fa riferimento a elementi di livello superiore utilizzando il prefisso "jaxb".

Come posso creare un file di binding JAXB valido?

risposta

8

http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd è sbagliato, ma nessuno può farci niente :(

Il problema è nella definizione di globalBindings elemento globale Ecco come si presenta:.

<xs:element name="globalBindings"> 
    <xs:annotation> 
     <xs:documentation>Customization values defined in global scope.</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:sequence minOccurs="0"> 
     <xs:element ref="jaxb:javaType" minOccurs="0" maxOccurs="unbounded" /> 
     <xs:element ref="jaxb:serializable" minOccurs="0" /> 
     <xs:any namespace="##other" processContents="lax"> 
      <xs:annotation> 
      <xs:documentation>allows extension binding declarations to be specified.</xs:documentation> 
      </xs:annotation> 
     </xs:any> 
     </xs:sequence> 
     ... 
    </xs:complexType> 

Ma dovrebbe essere simile questo:.

<xs:element name="globalBindings"> 
    <xs:annotation> 
     <xs:documentation>Customization values defined in global scope.</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:sequence minOccurs="0"> 
     <xs:element ref="jaxb:javaType" minOccurs="0" maxOccurs="unbounded" /> 
     <xs:element ref="jaxb:serializable" minOccurs="0" /> 
     <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"> 
      <xs:annotation> 
      <xs:documentation>allows extension binding declarations to be specified.</xs:documentation> 
      </xs:annotation> 
     </xs:any> 
     </xs:sequence> 
     ... 
    </xs:complexType> 

Mind the minOccurs="0" maxOccurs="unbounded" su <xs:any /> elemento

012.351.

Quindi la versione ufficiale forza per utilizzare l'elemento diverso da quello di JAXB globalBindings. Puoi guardare [http://jaxb.java.net/nonav/2.0/binding-customization/http.java.sun.com.xml.1306680588/index.html](http://java.sun.com/xml/ns/jaxb/xjc namespace) che contiene le estensioni di Sun a JAXB.

+0

Grazie, mio ​​test mostrano che ti sembra di essere corretto. Ho presentato un problema qui: http://java.net/jira/browse/JAXB-924 Sentiti libero di votare e/o commentare. – Puce

+0

votato e guardato :) –

+1

Qualsiasi soluzione alternativa? – l3dx

1

Apparentemente, il bug non è ancora stato risolto. Il suggerimento con <xsd:annotation><xsd:documentation>Use built in date conversion support</xsd:documentation> non ha funzionato per me, in quanto ho ottenuto un errore "namespace binding non supportato http://www.w3.org/2001/XMLSchema" (tradotto). Invece la seguente sintassi ha funzionato bene:

<jaxb:globalBindings> 
    <xjc:javaType 
    name="org.joda.time.LocalDate" 
    xmlType="xs:date" 
    adapter="org.example.XmlDateAdapter" /> 
</jaxb:globalBindings> 
0

per me ha funzionato per modificare il prefisso da xs a xsd. posso solo immaginare che la mia ragione è che il mio wsdl definisce lo spazio dei nomi con il prefisso xsd.

0

Come soluzione alternativa, è sufficiente aggiungere <xsd:any/> come ultimi figli di <globalBindings>

Ecco un esempio:

<globalBindings> 
    <javaType name="java.util.Calendar" xmlType="xsd:time" 
     parseMethod="javax.xml.bind.DatatypeConverter.parseTime" 
     printMethod="javax.xml.bind.DatatypeConverter.printTime" /> 
    <xsd:any/> 
</globalBindings> 

Questo funziona per me in STS 3.7.3 editor XML durante la convalida.

0

ho ottenuto lo stesso errore, e ho risolto dal cambiando il prefisso <javaType> elemento da jaxb (xmlns: jaxb = "http://java.sun.com/xml/ns/jaxb") per xjc (xmlns: xjc = "http://java.sun.com/xml/ns/jaxb/xjc").

Quindi, l'errore è stato visualizzato per questo codice:

<?xml version="1.0" encoding="UTF-8"?> 
<jaxb:bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
      http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd 
      http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd" 
      version="2.1"> 
    <jaxb:bindings schemaLocation="sci_paper_no_rdfa.xsd"> 
     <jaxb:globalBindings> 
      <jaxb:javaType name="java.util.Date" xmlType="xs:date" 
       parseMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.parseDate" 
       printMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.printDate"/> 
     </jaxb:globalBindings> 
    </jaxb:bindings> 
</jaxb:bindings> 

E il codice fisso è simile al seguente:

<?xml version="1.0" encoding="UTF-8"?> 
<jaxb:bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
      http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd 
      http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd" 
      version="2.1"> 
    <jaxb:bindings schemaLocation="sci_paper_no_rdfa.xsd"> 
     <jaxb:globalBindings> <!-- note that javaType now has xjc prefix --> 
      <xjc:javaType name="java.util.Date" xmlType="xs:date" 
       parseMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.parseDate" 
       printMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.printDate"/> 
     </jaxb:globalBindings> 
    </jaxb:bindings> 
</jaxb:bindings>