xml @xmlSchema 注释与 jaxb 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4863786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@xmlSchema annotation use with jaxb
提问by Fran
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:
我无法在 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;
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?
我做错了什么?我怎样才能得到预期的输出?
回答by bdoughan
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)
输出 - 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"/>

