【问题标题】:How do I get EclipseLink JAXB (MOXy) to not display namespaces in XML output如何让 EclipseLink JAXB (MOXy) 不在 XML 输出中显示命名空间
【发布时间】:2013-02-13 05:58:33
【问题描述】:

我正在尝试从 JAXB 参考实现转移到 EclipseLink JAXB (MOXy),因为它似乎可以解决 JAXB outputting invalid XML when data contains non-displayable chars 但我在显示命名空间标签时遇到了问题。

这就是我创建JAXBContext的方式

return JAXBContext.newInstance("org.musicbrainz.mmd2");

这是我得到的输出

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <metadata created="2013-02-27T12:12:13.305Z" 
        xmlns="http://musicbrainz.org/ns/mmd-2.0#" 
        xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
        <annotation-list count="1" offset="0">
            <annotation type="release" ext:score="100">
                <entity>bdb24cb5-404b-4f60-bba4-7b730325ae47</entity>
                <name>Pieds nus sur la braise</name>
                <text>EAN: 0828768226629 - DiscID: TWj6cLku360MfFYAq_MEaT_stgc-</text>
            </annotation>
        </annotation-list>
    </metadata>

我正在尝试使用 EclipseLink MOXy 获得相同的输出,我得到的上下文如下

 Map<String, Object> properties = new HashMap<String, Object>(1);
 properties.put(JAXBContextProperties.MEDIA_TYPE, "application/xml");
 return JAXBContextFactory.createContext(new Class[]{Metadata.class}, properties);

这会产生

<?xml version="1.0" encoding="UTF-8"?>
<ns0:metadata 
   xmlns:ns0="http://musicbrainz.org/ns/mmd-2.0#" 
   xmlns:ext="http://musicbrainz.org/ns/ext#-2.0" 
   created="2013-02-27T12:11:35.511Z">
   <ns0:annotation-list count="1" offset="0">
      <ns0:annotation type="release" ext:score="100">
         <ns0:entity>bdb24cb5-404b-4f60-bba4-7b730325ae47</ns0:entity>
         <ns0:name>Pieds nus sur la braise</ns0:name>
         <ns0:text>EAN: 0828768226629 - DiscID: TWj6cLku360MfFYAq_MEaT_stgc-</ns0:text>
      </ns0:annotation>
   </ns0:annotation-list>
</ns0:metadata>

我不想要 ns0 的东西,我可以摆脱它吗

【问题讨论】:

  • 您是如何在 JAXB 模型中定义命名空间信息的?

标签: xml jaxb eclipselink moxy


【解决方案1】:

问题 #1 - 使用默认命名空间

包裹信息

我们将使用包级别的@XmlSchema 注解来指定命名空间限定。我们还建议不要对 http://musicbrainz.org/ns/mmd-2.0# 命名空间使用前缀,而将 ext 前缀用于 http://musicbrainz.org/ns/ext#-2.0" 命名空间。

@XmlSchema(
    namespace="http://musicbrainz.org/ns/mmd-2.0#",
    xmlns={
        @XmlNs(namespaceURI="http://musicbrainz.org/ns/mmd-2.0#", prefix=""),
        @XmlNs(namespaceURI = "http://musicbrainz.org/ns/ext#-2.0", prefix = "ext")
    }
)
package forum15111903;

import javax.xml.bind.annotation.*;

元数据

http://musicbrainz.org/ns/mmd-2.0# 的域模型中不必包含命名空间信息,因为默认情况下它将应用于此包中的所有元素。

package forum15111903;

import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Metadata {

    @XmlAttribute
    private XMLGregorianCalendar created;

}

更多信息


问题 #2 - 将 MOXy 作为 JAXB (JSR-222) 提供者进行引导

选项 #1 - 使用标准 JAXB API

您可以在与模型类相同的包中包含一个名为 jaxb.properties 的文件,并使用以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

然后你可以引导你的JAXBContext如下:

package forum15111903;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Metadata.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15111903/input.xml");
        Metadata metadata = (Metadata) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(metadata, System.out);
    }

}

选项 #2 - 使用 MOXy 的原生 API

如果您不想使用 jaxb.properties 文件,则可以利用 MOXy JAXBContextFactory 类并执行以下操作:

package forum15111903;

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Metadata.class}, null);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15111903/input.xml");
        Metadata metadata = (Metadata) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(metadata, System.out);
    }

}

问题 #3 - properties.put(JAXBContextProperties.MEDIA_TYPE, "application/xml");

MOXy 的默认媒体类型为application/xml,您可以使用此属性指定application/json 以获取JSON 输出。

【讨论】:

    【解决方案2】:

    不要直接使用JAXBContextFactory,只需创建一个名为jaxb.properties 的文件,其中包含该行

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    并把它和你的Metadata.java放在同一目录下,然后JAXBContext.newInstance("org.musicbrainz.mmd2")会自动使用EclipseLink JAXB实现。

    【讨论】:

    • 我不希望能够在代码中在两者之间切换,也不希望部署文件的额外复杂性,但现在看着它我意识到也许我什至没有使用 eclipselink代码,我刚试过 return org.eclipse.persistence.jaxb.JAXBContextFactory.newInstance("org.musicbrainz.mmd2");但那是未知的方法调用
    • @PaulTaylor jaxb.properties 文件只会影响该特定包。但是,如果您不想弄乱属性文件,可以尝试JAXBContextFactory.createContext("org.musicbrainz.mmd‌​2", Metadata.class.getClassLoader()) 或三参数版本JAXBContextFactory.createContext("org.musicbrainz.mmd‌​2", Metadata.class.getClassLoader(), properties)
    【解决方案3】:

    如果您使用 xjc 生成 java 类,或者只是不想直接使用注释,您可以通过 MOXy 绑定文件 (oxm) 来实现:

    <?xml version="1.0"?>
    <xml-bindings
      xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
      package-name="your.package.name">
    
      <xml-schema
        namespace="http://yournamespace">
        <xml-ns
          prefix=""
          namespace-uri="http://yournamespace">
        </xml-ns>
      </xml-schema>
    </xml-bindings>
    

    这与 Blaise 在 package-info 的帖子中提出的建议相同。

    外部绑定文件的使用方法见this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      相关资源
      最近更新 更多