Использование аннотации @xmlSchema с jaxb - PullRequest
10 голосов
/ 01 февраля 2011

Не удается отобразить в файле XML все параметры, настроенные с помощью аннотации @xmlSchema на уровне пакета.Например, если я установлю:

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

Что яя делаю неправильно?Как я могу получить ожидаемый результат?

Ответы [ 2 ]

3 голосов
/ 01 февраля 2011

Вы можете записать расположение схемы следующим образом:

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

}

Выход - Метро (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"/>
1 голос
/ 03 февраля 2011

извините за задержку .... Спасибо за вашу помощь, теперь я могу показать schemaLocation, но у меня все еще нет xml, как хотелось бы. Возможно, я не объяснил сценарий должным образом с самого начала, позвольте мне повторить попытку:

У меня есть 2 схемы: CommonDataTypeCairo.xsd и StationNetwork.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, но я не знаю, как его получить. Было бы здорово, если бы вы могли помочь с этим.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...