简体   繁体   English

如何使用EclipseLink MOXy在JAXB中使用名称空间?

[英]How to use namespaces in JAXB using EclipseLink MOXy?

I have an XML like: 我有一个XML:

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

I am using @XmlPath mappings. 我正在使用@XmlPath映射。 but when I run the code I get this error: 但是当我运行代码时,我收到此错误:

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.

I wonder what am I missing? 我想知道我错过了什么?

Below is an example of how you can map your use case with EclipseLink JAXB (MOXy) . 下面是如何使用EclipseLink JAXB(MOXy)映射用例的示例

package-info 包信息

First you need to set up the namespace information using the package level @XmlSchema annotation. 首先,您需要使用包级别@XmlSchema注释设置命名空间信息。 We will leverage the namespace prefixes specified with the @XmlNs annotation later with the @XmlPath annotation. 我们将稍后利用@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.*; 

Message 信息

The @XmlPath annotation is used to specify an XPath based mapping with MOXy. @XmlPath注释用于指定基于XPath的MOXy映射。 Since in the @XmlSchema annotation we specified elementFormDefault=XmlNsForm.UNQUALIFIED , the portions of the XPath without a prefix will not be namespace qualified. 因为在@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 jaxb.properties

To specify MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain model with the following entry (see http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ): 要将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

Demo 演示

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 input.xml中/输出

<?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>

For More Information 欲获得更多信息

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

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