2011-02-01 20 views
10

パッケージレベルで@xmlSchemaアノテーションで設定されたすべてのパラメータをxmlファイルに表示することができません。例えば、私が設定した場合、:@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"/> 

出力 -をあなたの助けのために遅れて申し訳ありません

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

+0

心配しないで、package-info.javaの@XmlSchemaはうまくいきました。 – neu242

1

....おかげで、今私はのschemaLocationを表示することができますが、私が望むように私はまだXMLを持っていません。たぶん私は最初から適切なシナリオを説明していない、私はもう一度試してみましょう:CommonDataTypeCairo.xsdと共通の構造を使用する前のものを輸入StationNetwork.xsd

私は2つのスキーマを持っています。

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のCを持っています私は別の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。有効ではありません

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

、そして私が欲しいの出力は次のようになります:私はこの構成で取得するXMLがあるNS3接頭辞なし

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

が、私はそれを取得する方法がわかりません。もしあなたがこれを手伝ってくれれば素晴らしいかもしれません。

関連する問題