使用多个名称空间解组 [英] Unmarshalling with multiple namespaces

查看:68
本文介绍了使用多个名称空间解组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,让我说这个xml有几个命名空间。

So, lets say I have this xml with several namespaces.

<Envelope xmlns:pdi="http://www.mypage.com/schemas/pdi" xmlns:ib="http://www.mypage.com/schemas/ib" xmlns="http://www.mypage.com/schemas/envelope">
  <Product>
    <pdi:number>123456</pdi:number>
  </Product>
  <Instance>
    <ib:serial>abcdefg</ib:serial>
  </Instance>
</Envelope>

我正在尝试为它构建一个客户端。我有一个声明为这样的信封POJO

I'm trying to build a client for it. I have an Envelope POJO that's declared like this

@XmlRootElement(name ="Envelope", namespace = "http://www.mypage.com/schemas/envelope")
public class Envelope

和里面,它具有这些属性

and inside, it has these attributes

@XmlElement(name="Product", namespace = "http://www.mypage.com/schemas/pdi")
public Product getProduct(){...}

@XmlElement(name="Instance", namespace = "http://www.mypage.com/schemas/ib")
public Instance getInstance(){...}

此外,产品POJO看起来像这样:

Also, the Product POJO looks like this:

@XmlRootElement(name="Product", namespace = "http://www.mypage.com/schemas/pdi")
public class Product

和属性

@XmlElement(name="pdi:number", namespace = "http://www.mypage.com/schemas/pdi")
public int getNumber(){...}

出于某种原因,我无法得到产品编号。我一直收到请求错误。我是否正确处理命名空间,或者我错过了什么?

For some reason, I can't get the product number. I keep getting a request error. Am I handling the namespaces correctly, or am I missing something?

推荐答案

对于这个用例,我建议利用包级别 @XmlSchema 注释以指定命名空间限定。

For this use case I would recommend leveraging the package level @XmlSchema annotation to specify the namespace qualification.

package-info(forum14651918 / package-info.java )

@XmlSchema(
    namespace="http://www.mypage.com/schemas/envelope", 
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns={
            @XmlNs(namespaceURI = "http://www.mypage.com/schemas/envelope", prefix = ""),
            @XmlNs(namespaceURI = "http://www.mypage.com/schemas/ib", prefix = "ib"),
            @XmlNs(namespaceURI = "http://www.mypage.com/schemas/pdi", prefix = "pdi")
    }
)
@XmlAccessorType(XmlAccessType.FIELD)
package forum14651918;

import javax.xml.bind.annotation.*;

信封(forum14651918 / Envelope.java)

因为我们在命名空间和 elementFormDefault > @XmlSchema 注释,对应 Envelope 类的所有元素将自动通过 http:// www进行限定.mypage.com / schemas / envelope namespace。

Since we have specified a namespace and elementFormDefault on the @XmlSchema annotation, all the elements corresponding to the Envelope class will be automatically qualified with the http://www.mypage.com/schemas/envelope namespace.

package forum14651918;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="Envelope")
public class Envelope {

    @XmlElement(name="Product")
    private Product product;

    @XmlElement(name="Instance")
    private Instance instance;

}

产品(forum14651918 / Product.java)

您可以使用 @XmlType覆盖 Product 类的命名空间注释。

You can override the namespace for the Product class using the @XmlType annotation.

package forum14651918;

import javax.xml.bind.annotation.*;

@XmlType(namespace="http://www.mypage.com/schemas/pdi")
public class Product {

    private int number;

}

实例(forum14651918 / Instance.java)

您可以使用 @XmlType覆盖 Instance 类的命名空间注释。

You can override the namespace for the Instance class using the @XmlType annotation.

package forum14651918;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace="http://www.mypage.com/schemas/ib")
public class Instance {

    private String serial;

}

演示(forum14651918 / Demo.java)

以下是您可以运行的一些代码,以证明一切正常。

Below is some code you can run to prove that everything works.

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14651918/input.xml");
        Envelope envelope = (Envelope) unmarshaller.unmarshal(xml);

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

}

更多信息

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
  • http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

这篇关于使用多个名称空间解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆