使用 JAXB 解组

发布于 2024-12-18 00:05:47 字数 986 浏览 4 评论 0原文

我是 java 新手(来自 c#.net 背景),并尝试使用上面的示例来编组和解组。

按照下面的链接 使用 JaxB 编组实现通用接口的对象列表

使用 Blaise Doughan 先生提到的上述技术,我能够将 java 对象封送到 xml。但是,当我保存此 xml 并尝试将 xml 解组回 java 对象时,我在控制台上看到以下内容:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:2 IllegalAnnotationException 的计数 @javax.xml.bind.annotation.XmlElementRef 注释在两个上找到 地方;一个就足够了。这个问题与以下内容有关 位置:@javax.xml.bind.annotation.XmlElementRef(name=##default, 必需=true,类型=类 javax.xml.bind.annotation.XmlElementRef$DEFAULT, 命名空间=) 在公共 java.util.List Community.getPeople() 在 Community 这个问题是 与以下位置相关:位于 @javax.xml.bind.annotation.XmlElementRef(name=##default, 必需=true,类型=类 javax.xml.bind.annotation.XmlElementRef$DEFAULT, 命名空间=) 在公共 void Community.setPeople(java.util.List) 在社区....

注意:我为 Class Boy 和 Class Girl 创建了 getter/setter 来实现解组。

I am new to java (coming from c#.net background) and was trying the above example to marshal and unmarshal.

Following the link below
Marshalling a List of objects implementing a common interface, with JaxB

using the above technique as mentioned by Mr.Blaise Doughan, I was able to marshal the java objects to xml. But when i save this xml and try to unmarshal the xml back to java object i get the following on the console:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2
counts of IllegalAnnotationExceptions
@javax.xml.bind.annotation.XmlElementRef annotation is found on two
places; one would be suffice. this problem is related to the following
location: at @javax.xml.bind.annotation.XmlElementRef(name=##default,
required=true, type=class
javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public
java.util.List Community.getPeople() at Community this problem is
related to the following location: at
@javax.xml.bind.annotation.XmlElementRef(name=##default,
required=true, type=class
javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public
void Community.setPeople(java.util.List) at Community ....

Note : I have created getters/setters for Class Boy and Class Girl for implementing unmarshalling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-12-25 00:05:47

看来您可能已经注释了 getPeople 和 setPeople 方法。 JAXB(和其他 Java EE 技术)只需要您注释一个。

public class Community {

    private List<Person> people;

    @XmlElementRef
    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    } 

}

了解更多信息

It appears that you may have annotated both the getPeople and setPeople methods. JAXB (and other Java EE technologies) only require you annotate one.

public class Community {

    private List<Person> people;

    @XmlElementRef
    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    } 

}

For More Information

自在安然 2024-12-25 00:05:47

如果您展示了代码,那么提供帮助会更容易...

问题似乎是,您有 getter 和 setter,这让 JAXB 感到困惑,因为它不知道如何使用它们来解组 xml。

尝试使用 FIELD 访问类型:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Blubb", namespace=ServiceConstants.XML_NAMESPACE)
public class Blubb implements Serializable {

    @XmlElement(name="Bla", namespace=ServiceConstants.XML_NAMESPACE)
    private Bla bla;

    public Blubb () {

    }

    public void setBla(Bla bla) { this.bla = bla; }

    public Bla getBla() { return this.bla; }
}

It's easier to help, if you showed your code...

The problems seems to be, that you have getters and setters and which confuses JAXB, because it does not know how to use them to unmarshal the xml.

Try to use FIELD access type:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Blubb", namespace=ServiceConstants.XML_NAMESPACE)
public class Blubb implements Serializable {

    @XmlElement(name="Bla", namespace=ServiceConstants.XML_NAMESPACE)
    private Bla bla;

    public Blubb () {

    }

    public void setBla(Bla bla) { this.bla = bla; }

    public Bla getBla() { return this.bla; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文