@xmlSchema 注释与 jaxb 一起使用 [英] @xmlSchema annotation use with jaxb

查看:47
本文介绍了@xmlSchema 注释与 jaxb 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 xml 文件中显示在包级别使用 @xmlSchema 注释配置的所有参数.例如,如果我设置:

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;

我希望看到如下内容:

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

我做错了什么?我怎样才能得到预期的输出?

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

推荐答案

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

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

运行以下代码:

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

这篇关于@xmlSchema 注释与 jaxb 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆