2011-02-01 537 views
10

我无法在xml文件中显示在包级别使用@xmlSchema注释配置的所有参数。例如,如果我设置:@xmlSchema注释与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; 

我希望看到这样的:

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

,但我得到以下输出:

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

我做错了吗?我怎样才能得到预期的输出?

回答

3

如下您可以编写出一个架构位置:

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

运行下面的代码:

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

} 

输出 - 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"/> 

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

我该如何设置带有注释的根节点上的xmlns?我使用Response.ok(entity).build()从我的@Get方法返回,并且根本没有直接使用编组器。 – neu242 2011-06-01 09:14:11

1

抱歉耽搁....感谢您的帮助,我现在可以显示的schemaLocation,但我仍然不具备XML,因为我想。也许我并没有从一开始就正确解释的情况下,让我再试一次:

我有2种模式:CommonDataTypeCairo.xsdStationNetwork.xsd其中进口前一个使用常见的结构。

CommonDataTypeCairo.xsd开始如下:

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

而且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"/> 

我有绑定的JavaÇ在不同的软件包中,所以我有不同的package-info.java文件。对于StationNetwork模式,我有:

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

和公共模式:

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

StationNetwork。XML我这个配置得到的是:

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

这是不合法的,我想输出是:

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

没有NS3前缀,但我不知道如何得到它。如果你能在这方面提供帮助,这可能会很好。