2011-02-01 2 views
10

Non riesco a visualizzare in un file xml tutti i parametri configurati con l'annotazione @xmlSchema a livello di pacchetto. Per esempio, se ho impostato:@xmlSchema annotazione utilizzata con jaxb

@javax.xml.bind.annotation.XmlSchema (    
    xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "com", 
        namespaceURI="http://es.indra.transporte.common"), 

      @javax.xml.bind.annotation.XmlNs(prefix = "xsi", 
        namespaceURI="http://www.w3.org/2001/XMLSchema-instance"), 

      @javax.xml.bind.annotation.XmlNs(prefix = "ns2", 
        namespaceURI="http://es.indra.transporte.configuration"),    
      },  
    location = "http://es.indra.transporte.configuration StationNetwork.xsd", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED   
) 
package es.indra.transporte.central.thalesinterface.common.beans; 

mi aspetto di vedere qualcosa di simile:

<stationNetwork xmlns:ns2="http://es.indra.transporte.configuration" 
       xmlns:com="http://es.indra.transporte.common" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://es.indra.transporte.configuration StationNetwork.xsd"> 

ma ottengo il seguente output:

<stationNetwork xmlns:com="http://es.indra.transporte.common"> 

Quello che sto facendo di sbagliato? Come posso ottenere l'output atteso?

risposta

3

È possibile scrivere un posizione dello schema come segue:

Marshaller marshaller = jc.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://es.indra.transporte.configuration StationNetwork.xsd"); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(root, System.out); 

L'esecuzione del codice seguente:

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(StationNetwork.class); 

     StationNetwork root = new StationNetwork(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://es.indra.transporte.configuration StationNetwork.xsd"); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(root, System.out); 
    } 

} 

uscita - Metro (JAXB RI)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<stationNetwork 
    xmlns:com="http://es.indra.transporte.common" 
    xmlns:ns2="http://es.indra.transporte.configuration"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://es.indra.transporte.configuration StationNetwork.xsd"/> 

Uscita - EclipseLink JAXB (MOXy)

<?xml version="1.0" encoding="UTF-8"?> 
<stationNetwork 
    xsi:schemaLocation="http://es.indra.transporte.configuration StationNetwork.xsd" 
    xmlns:ns2="http://es.indra.transporte.configuration" 
    xmlns:com="http://es.indra.transporte.common" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
+0

Come faccio a impostare gli xmlns sul nodo radice con le annotazioni? Ritorno dal mio metodo @Get con Response.ok (entity) .build(), e non sto usando direttamente il marshaller. – neu242

+0

Non importa, @XmlSchema in package-info.java funzionava alla grande. – neu242

1

scusate il ritardo .... Grazie per il vostro aiuto, ora posso mostrare lo schemaLocation, ma io ancora non hanno la xml come vorrei. Forse non mi sono spiegato lo scenario correttamente fin dall'inizio, mi permetta di provare di nuovo:

Ho 2 schemi: CommonDataTypeCairo.xsd e StationNetwork.xsd che importa quello precedente di usare strutture comuni.

Il CommonDataTypeCairo.xsd inizia come segue:

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
     xmlns:com="http://es.indra.transporte.common" 
     targetNamespace="http://es.indra.transporte.common" 
     elementFormDefault="qualified" 
     attributeFormDefault="unqualified"> 
    <complexType name="head">   
     <sequence>    
      <element name="formatVersion" type="integer"/> 
     <element name="confVersion" type="integer"/>    
      <element name="generationDate" type="dateTime"/>        
      <element name="activationDate" type="dateTime"/>   
     </sequence> 
    </complexType> 

E il StationNetwork.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:com="http://es.indra.transporte.common" 
      xmlns="http://es.indra.transporte.configuration" 
      targetNamespace="http://es.indra.transporte.configuration" 
      lementFormDefault="qualified" attributeFormDefault="unqualified"> 
    <xs:import namespace="http://es.indra.transporte.common" 
       schemaLocation="CommonDataTypeCairo.xsd"/> 

devo il limite java c lasses in diversi pacchetti quindi ho diversi file package-info.java. Per lo schema StationNetwork ho:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://es.indra.transporte.configuration"     
) 
package es.indra.transporte.central.thalesinterface.topology.beans; 

e per lo schema comune:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://es.indra.transporte.common", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, 
    attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED 
) 
package es.indra.transporte.central.thalesinterface.common.beans; 

Il StationNetwork.xml ottengo con questa configurazione è:

<ns3:stationNetwork xmlns:ns2="http://es.indra.transporte.common" 
        xmlns:ns3="http://es.indra.transporte.configuration" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://es.indra.transporte.configuration StationNetwork.xsd"> 
<head> 
    <ns2:formatVersion>1</ns2:formatVersion> 
    <ns2:confVersion>1</ns2:confVersion> 
    <ns2:generationDate>2010-12-22T00:00:00Z</ns2:generationDate> 
    <ns2:activationDate>2010-12-21T09:07:25Z</ns2:activationDate> 
</head> 

che non è valido, e l'uscita che voglio è:

<stationNetwork xmlns:ns2="http://es.indra.transporte.common" 
       xmlns="http://es.indra.transporte.configuration" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://es.indra.transporte.configuration StationNetwork.xsd"> 
<head> 
    <ns2:formatVersion>1</ns2:formatVersion> 
    <ns2:confVersion>1</ns2:confVersion> 
    <ns2:generationDate>2010-12-22T00:00:00Z</ns2:generationDate> 
    <ns2:activationDate>2010-12-21T09:07:25Z</ns2:activationDate> 
</head> 

senza prefisso NS3, ma non so come ottenerlo. Potrebbe essere fantastico se puoi aiutare in questo.