【问题标题】:@xmlSchema annotation use with jaxb@xmlSchema 注释与 jaxb 一起使用
【发布时间】:2011-06-19 07:48:36
【问题描述】:

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

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

【问题讨论】:

    标签: xml schema annotations jaxb


    【解决方案1】:

    您可以如下写出架构位置:

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

    输出 - 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?我使用 Response.ok(entity).build() 从我的 @Get 方法返回,我根本没有直接使用编组器。
    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2018-09-26
    • 2011-06-20
    • 2019-10-14
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多