您现在的位置是:首页 > 编程开发 > Java > Java

java – 具有多个名称空间的解组

编辑:站长来源:网络整理作者: 未知2020-12-15Java

简介所以,假设我有几个名称空间的xml. 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:number123456/pdi:number /Product Instance ib:seri

所以,假设我有几个名称空间的xml.

<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

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

在里面,它有这些属性

@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如下所示:

@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(){...}

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

解决方法

对于此用例,我建议利用包级别@XmlSchema注释来指定命名空间限定.

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)

由于我们在@XmlSchema批注上指定了名称空间和elementFormDefault,因此将使用http://www.mypage.com/schemas/envelope名称空间自动限定与Envelope类对应的所有元素.

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类的命名空间.

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类的命名空间.

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)

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

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

很赞哦! ()

点击排行

本栏推荐

猜你喜欢