@xmlSchema annotation use with jaxb

2019-04-05 10:30发布

问题:

I can't get to show in a xml file all the parameters configured with the @xmlSchema annotation at package level. For example, if I set:

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

I expect to see something like:

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

but I get the following output:

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

What I'm doing wrong? How can I get the expected output?

回答1:

You can write out a schema location as follows:

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

Running the following code:

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

}

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

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


回答2:

sorry for the delay....Thanks for your help, now I can show the schemaLocation, but I still don't have the xml as I would like. Maybe I didn't explain the scenario properly from the beginning, let me try again:

I have 2 schemas: CommonDataTypeCairo.xsd and StationNetwork.xsd which imports the previous one to use common structures.

The CommonDataTypeCairo.xsd starts as follows:

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

And the 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"/>

I have the bound java classes in different packages so I have different package-info.java files. For the StationNetwork schema I have:

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

and for Common schema:

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

The StationNetwork.xml I get with this configuration is:

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

which is not valid, and the output I want is:

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

without ns3 prefix, but I don't know how to get it. It could be great if you can help on this.