JAXB解组中XML中的命名空间问题 [英] Issue with namespaces in XMLs in JAXB unmarshalling

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

问题描述

我有一个XML来解组JAXB。如果我从元素中删除所有命名空间属性,但是如果我保留命名空间属性,则在解组后得到一个空对象,代码工作正常。

I have an XML to unmarshall with JAXB. The code works fine if I remove all namespace attributes from the elements but I get a null object after unmarshalling if I keep the namespace attributes.

XML是这样的:

<Animal  xmlns="http://allmycats.com/serviceplatform/1.0/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Cat z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty</name>
</Cat>
<Cat z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty2</name>
</Cat>
</Animal>

My Animal bean是这样的:

My Animal bean is like this:

    @XmlRootElement(name = "Animal")
public class Animal{
    List<Cat> cats;

    @XmlElement(name = "Cat")
    public List<Cat> getCats() {
        return cats;
    }

    public void setCats(List<Cat>cats) {
        this.cats= cats;
    }
}

Cats bean就像:

The Cats bean is like:

@XmlRootElement(name = "Cat")
public class Cat {
    private String zId;

    @XmlAttribute(name = "z:Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
    public String getzId() {
        return zId;
    }
    public void setzId(String zId) {
        this.zId = zId;
    }

    private String name;
    @XmlElement(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

在运行时,我得到一个空对象。我试图从属性中删除z:,我得到了这个例外:

At runtime, I get an empty object. I tried to remove "z:" from the attribute and I got this exception:

org.xml.sax.SAXParseException; The prefix "z" for attribute "z:Id" associated with an element type "Cat" is not bound.]

如果我从cat和Animal中删除命名空间,我会得到以下异常:

If I remove namespaces from cat and Animal, I get this exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://allmycats.com/serviceplatform/1.0/", local:"Animal"). Expected elements are <{}Animal>

unmarshall的最终代码如下。最后一行给出空指针异常

Final code to unmarshall is below. The last line gives a null pointer exception

File file = new File(filepath1);
System.out.println("file exists? : "+ file.exists()); // prints true

JAXBContext jaxbContext = JAXBContext.newInstance(Animal2.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Animal2 animals = (Animal2)jaxbUnmarshaller.unmarshal( file );
System.out.println("--file size: "+animals.getCats().size());

我不知道如何处理命名空间和 z:属性。有什么建议吗?

I am not sure how to handle the namespaces and the z: attributes in my POJO classes. Any suggestions please ?

如果我在XML中没有任何命名空间或任何带有命名空间的属性,但是我无法改变XML。

The code works fine if I do not have any namespace or any attribute with namespace in the XML but I cannot alter the XML.

推荐答案

XMLAtribute有一个属性namesape,所以

XMLAtribute has atribute namesape, so

@XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization").

在用xml判断时,cat与动物位于同一名称空间中

While judging by your xml, the cat is in the same namespace as the animal so

以下代码适用于JDK 7(修复了ns的动态和属性名称为zid)。

The following code works with JDK 7 (fixed the ns for animal and attribute name for zid).

@XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
public class Animal2{
    List<Cat2> cats;

    @XmlElement(name = "Cat")
    public List<Cat2> getCats() {
        return cats;
    }

    public void setCats(List<Cat2>cats) {
        this.cats= cats;
    }
}
@XmlRootElement(name = "Cat")
public class Cat2 {
    private String zId;

    @XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
    public String getzId() {
        return zId;
    }
    public void setzId(String zId) {
        this.zId = zId;
    }

    private String name;

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

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

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