@xmlSchema注释在jaxb中的使用

10

我无法在一个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">

我做错了什么?我该如何获得预期的输出结果?

1个回答

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

}

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

我该如何在根节点上使用注释设置xmlns?我的@Get方法通过Response.ok(entity).build()返回,我根本没有直接使用marshaller。 - neu242

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接