2011-02-01 35 views
10

No puedo mostrar en un archivo xml todos los parámetros configurados con la anotación @xmlSchema en el nivel del paquete. Por ejemplo, si fijo:uso de la anotación @xmlSchema 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; 

espero ver algo como:

<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"> 

pero me da el siguiente resultado:

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

Lo que estoy haciendo mal? ¿Cómo puedo obtener el resultado esperado?

Respuesta

3

Puede escribir un esquema de ubicación de la siguiente manera:

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); 

Ejecutar el siguiente código:

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); 
    } 

} 

salida - 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"/> 

salida - 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

¿Cómo puedo establecer los xmlns en el nodo raíz con anotaciones? Regreso de mi método @Get con Response.ok (entidad) .build(), y no estoy usando el Marshaller directamente. – neu242

+0

No importa, @XmlSchema en package-info.java funcionó muy bien. – neu242

1

siento por el retraso .... Gracias por su ayuda, ahora pueden mostrar la schemaLocation, pero todavía no tienen el XML como me gustaría. Tal vez no me explico el escenario correctamente desde el principio, voy a tratar de nuevo:

tengo 2 esquemas: CommonDataTypeCairo.xsd y StationNetwork.xsd que importa la anterior de utilizar estructuras comunes.

El CommonDataTypeCairo.xsd comienza de la siguiente manera:

<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> 

Y el 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"/> 

que tienen la cota c java lasses en diferentes paquetes, así que tengo diferentes archivos package-info.java. Para el esquema StationNetwork tengo:

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

y para el esquema común:

@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; 

El StationNetwork.xml consigo con esta configuración es:

<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> 

que no es válida, y la salida que quiero es:

<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> 

sin prefijo NS3, pero no sé cómo conseguirlo. Podría ser genial si puedes ayudar con esto.

Cuestiones relacionadas