与JAXB @xmlSchema标注使用 [英] @xmlSchema annotation use with jaxb

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

问题描述

我不能让一个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;

我希望看到这样的:

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?

推荐答案

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

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

运行以下code:

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

}

输出 - 地铁(JAXB RI)

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

输出 - 的EclipseLink JAXB(莫西)

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

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

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