Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@xmlSchema annotation use with jaxb

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?

like image 572
Fran Avatar asked Feb 01 '11 14:02

Fran


1 Answers

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"/>
like image 158
bdoughan Avatar answered Oct 07 '22 11:10

bdoughan