简体   繁体   English

如何使用JAXB(JDK 7)设置默认名称空间

[英]How to set default namespace with JAXB (JDK 7)

I've got problem with setting default namespace with JAXB. 我在用JAXB设置默认名称空间时遇到问题。 I have followed tips from here but problem still exist. 我从这里开始遵循技巧,但问题仍然存在。 Here is how looks like my package-info: 这是我的package-info的样子:

@XmlSchema(
        namespace = "my/xml/namespace",
        xmlns = {
                @XmlNs(namespaceURI="my/xml/namespace",prefix=""),
        },
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package my.xml.namespace;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.XMLConstants;;

But still created xml looks like this: 但是仍然创建的xml看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:resp xmlns:ns2="my/xml/namespace">
....
</ns2:resp>

I would like to get rid of these prefix but run out of ideas. 我想摆脱这些前缀,但是用尽了所有想法。 Anyone maybe know why? 有人知道为什么吗?

UPDATE 更新

I habe followed advice and implement NamespacePrefixMapper and: 我遵循建议并实现NamespacePrefixMapper和:

  1. It's crucial to be com.sun.xml.bind.marshaller.NamespacePrefixMapper. com.sun.xml.bind.marshaller.NamespacePrefixMapper是至关重要的。 I've use another one and get wierd exceptions. 我使用了另一个,并得到了奇怪的例外。

  2. It does not work too - the same issue persist. 它也不起作用-相同的问题仍然存在。 Here is my mapper: 这是我的映射器:

    NamespacePrefixMapper mapper = new NamespacePrefixMapper(){ NamespacePrefixMapper映射器=新的NamespacePrefixMapper(){

      @Override public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { if("amc/xml/pay03_entryresponse".equals(namespaceUri)) { return ""; }else return suggestion; } }; marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM