【问题标题】:How to use namespaces in JAXB using EclipseLink MOXy?如何使用 EclipseLink MOXy 在 JAXB 中使用命名空间?
【发布时间】:2012-05-11 08:56:00
【问题描述】:

我有一个类似的 XML:

<message  xmlns:gtm="http:// www.example.com/working/gtm">
    <gtm:header>
    <someid></someid>
    <sometext></sometext>
    </gtm:header>
    <gtm:customer>0123456789</gtm:customer>
</message>

我正在使用@XmlPath 映射。但是当我运行代码时,我得到了这个错误:

Exception [EclipseLink-25016] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A namespace for the prefix gtm:header was not found in the namespace resolver.

我想知道我错过了什么?

【问题讨论】:

    标签: java jaxb eclipselink moxy


    【解决方案1】:

    下面是一个示例,说明如何使用 EclipseLink JAXB (MOXy) 映射您的用例。

    包裹信息

    首先您需要使用包级别的@XmlSchema 注解设置命名空间信息。稍后我们将利用@XmlNs 注释指定的命名空间前缀和@XmlPath 注释。

    @XmlSchema(
        namespace="http:// www.example.com/working/gtm",
        xmlns={
            @XmlNs(prefix="gtm", namespaceURI="http:// www.example.com/working/gtm")
        },
        elementFormDefault=XmlNsForm.UNQUALIFIED)
    package forum10548370;
    
    import javax.xml.bind.annotation.*; 
    

    留言

    @XmlPath 注释用于指定基于 XPath 的 MOXy 映射。由于在@XmlSchema 注释中我们指定了elementFormDefault=XmlNsForm.UNQUALIFIED,XPath 中没有前缀的部分将不会被命名空间限定。

    package forum10548370;
    
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="message", namespace="")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Message {
    
        @XmlPath("gtm:header/someid/text()")
        private String id;
    
        @XmlPath("gtm:header/sometext/text()")
        private String text;
    
        @XmlElement(namespace="http:// www.example.com/working/gtm")
        private String customer;
    
    }
    

    jaxb.properties

    要将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中添加一个名为 jaxb.properties 的文件,并使用以下条目(请参阅 http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

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

    演示

    package forum10548370;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Message.class);
    
            File xml = new File("src/forum10548370/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Message message = (Message) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(message, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <message xmlns:gtm="http:// www.example.com/working/gtm">
       <gtm:header>
          <someid></someid>
          <sometext></sometext>
       </gtm:header>
       <gtm:customer>0123456789</gtm:customer>
    </message>
    

    更多信息

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2014-12-23
      • 2013-09-06
      • 2014-11-07
      相关资源
      最近更新 更多