本文介绍了如何让EclipseLink JAXB(MOXy)不在XML输出中显示名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JAXB参考实现迁移到EclipseLink JAXB(MOXy),因为它似乎解决了,但我显示它显示命名空间标记时出现问题。

I'm trying to move from the JAXB reference implementation to EclipseLink JAXB (MOXy) because it appears to solve JAXB outputting invalid XML when data contains non-displayable chars but I have a problem with it displaying namespace tags.

这就是我创建 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获得相同的输出,我得到如下的上下文

I'm trying to get same output with EclipseLink MOXy, I get context as follows

 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的东西,我可以摆脱它吗

I don't want the ns0 stuff, can I get rid of it

推荐答案

问题#1 - 使用默认纳粹空间

package-info

我们将使用包级别 @XmlSchema 注释来指定命名空间资格。我们还建议 http://musicbrainz.org/ns/mmd-2.0# 命名空间不使用前缀,并且 ext 前缀用于 http://musicbrainz.org/ns/ext#-2.0命名空间。

We will use the package level @XmlSchema annotation to specify the namespace qualification. We will also suggest that no prefix be used for the http://musicbrainz.org/ns/mmd-2.0# namespace and and that the ext prefix be used for the http://musicbrainz.org/ns/ext#-2.0" namespace.

@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# 的名称空间信息,因为它默认情况下将应用于此包中的所有元素。

No namespace information has to be included on your domain model for the http://musicbrainz.org/ns/mmd-2.0# as it will be applied to all elements in this package by default.

package forum15111903;

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

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

    @XmlAttribute
    private XMLGregorianCalendar created;

}

更多信息



  • http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html

问题#2 - 作为JAXB(JSR-222)PROVIDER的BOOTSTRAPPING MOXY

选项#1 - 使用标准JAXB API

您可以包含一个名为的文件jaxb.properties 与您的模型类相同的包中包含以下条目(请参阅:):

You can include a file called jaxb.properties in the same package as your model classes with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

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

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

Then you can bootstrap your JAXBContext as follows:

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 类并执行以下操作:

If you don't want to use a jaxb.properties file then you can leverage the MOXy JAXBContextFactory class and do the following:

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

}






ISSUE#3 - properties.put(JAXBContextProperties.MEDIA_TYPE,application / xml);

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

The default media type for MOXy is application/xml, you can use this property to specify application/json to get JSON ouput.



  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

这篇关于如何让EclipseLink JAXB(MOXy)不在XML输出中显示名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:54