JAXB Unmarshalls XML错误 [英] JAXB Unmarshalls XML Incorrectly

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

问题描述

好吧,我是JAXB的新手并且无法找到正确的文档来帮助我解决这个问题,我已经阅读了很多文章但仍然不明白JaxB在做什么。



<我有一个课,称之为容器。它由MyContainer扩展。除了从Container继承的内容之外,MyContainer还有一个额外的布尔值。



无论如何,这就是JAXB Marshalls MyContainer

 <? xml version =1.0encoding =UTF-8standalone =yes?> 
< MyContainer name =containerid =1>
< boolVal> false< / boolVal>
< ListOfResources> //使用@XmlElementWrapper(name = ..
< child>
< ID> Test Comp 1< / ID>
< / child>
< child>
< ID> Test Comp 2< / ID>
< / child>
< / ListOfResources>
< / MyContainer>

我不明白的是标​​签child。唯一名为child的是列表的名称。列表包含对象(其中有一个字符串属性,ID)但是该对象不应该是child。然而真正的问题是当我去Demarshall这个班级时。列表将填充列出的孩子们。我不知道我错过了什么请注意,我没有获得JAXB异常,但是当我检查未编组的对象时,列表未填充



编辑:澄清



对不起,我要说的是JAXB将对列表中的元素进行编组,尽管列表中的每个元素都有列表的名称(子)。然而,当它进入demarshall时,它不会填充新对象中的列表。



EDIT2:当前的Setter

  public void setChildren(List< Resource< IType>>其他){
Iterator iterator = other.iterator();
while(iterator.hasNext()){
资源< IType> piece =
new资源< IType>((资源< IType>)iterator.next());
this.listOfResources.add(piece);
}

}


解决方案

我不确定你的问题是什么。首先你说列表将填充孩子,然后你说列表没有填充。它是什么,它与你期望看到的有什么不同?如果没有填充某些内容,最常见的原因是您没有为该属性创建格式良好的setter。



在命名时,元素是始终与映射到它的属性的名称相同。您可以使用 @XmlElement更改元素的名称(name =...),或者如果您希望在属性引用的对象之后命名元素(如组成列表的对象),请使用 @ XmlElementRef


Alright, I am new to JAXB and having trouble finding the correct documentation to help me through this, I have read a bunch and still dont understand what JaxB is doing.

I have a class, Call it Container. It is extended by MyContainer. MyContainer has an additional boolean value in addition to what it inherits from Container.

Anyways, this is how JAXB Marshalls MyContainer

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyContainer name="container" id="1">
    <boolVal>false</boolVal>
    <ListOfResources> //Using @XmlElementWrapper(name=..
        <child>
            <ID>Test Comp 1</ID>
        </child>
        <child>
            <ID>Test Comp 2</ID>
        </child>
    </ListOfResources>
</MyContainer>

What I don't understand is the tag "child". The only thing named child is the name of the list. The list contains objects (Which have a string property, ID) however the object should not be "child" . The real issue however is when I go to Demarshall the class. The list will populate with the listed "childs". I dont see what I am missing. I don't get a JAXB exception, mind you, but I when I check the unmarshalled object the list is unpopulated

EDIT: Clarification

I am sorry, What I am saying is the JAXB will marshall the elements within the list, albeit each element in the list having the name of the list (child). When it goes to demarshall, however, it does not populate the list in the new object.

EDIT2: Current Setter

public void setChildren(List<Resource<IType>> other) {
    Iterator iterator = other.iterator();
    while(iterator.hasNext()){
        Resource<IType> piece = 
            new Resource<IType>((Resource<IType>) iterator.next());
        this.listOfResources.add(piece);
    }

}

解决方案

I'm not sure what your problem is. First you said that the list will be populated with the "childs", then you say that the list is unpopulated. Which is it, and how does it differ from what you expect to see? If something isn't getting populated, the most common cause is that you don't have a well-formed setter for that property.

On the naming, an element is always named the same as the property that maps to it. You can change an element's name with @XmlElement(name="..."), or if you want the element to be named after the object referenced by the property (like the objects that make up a list), use @XmlElementRef.

这篇关于JAXB Unmarshalls XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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