[英]Jaxb2Marshaller and attributes
I have a problem using Jaxb2Marshaller for unmarshalling XML attributes (of primitive types). 我使用Jaxb2Marshaller来解组XML属性(原始类型)时遇到问题。 Here is the sample:
这是样本:
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns="...">
<items>
<item id="1"/>
<item id="2"/>
<item id="3"/>
</items>
</request>
And the entity class is: 实体类是:
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "request", namespace = "...")
@XmlType(name = "Request", namespace = "...")
public class Request {
private List<Item> _items;
@XmlElementWrapper(name = "items", namespace = "...")
@XmlElement(name = "item", namespace = "...")
public List<Item> getItems() {
return _items;
}
public void setItems(List<Item> items) {
_items= items;
}
@XmlType(name = "Item", namespace = "...")
public static class Item {
private Long _id;
@XmlAttribute(name = "id", namespace = "...")
public Long getId() {
return _id;
}
public void setId(Long id) {
_id = id;
}
}
}
After unmarshalling I have request.getItems().iterator().next().getId() == null while it should be 1. If I use nested elements instead of attributes everything works just fine. 在解组后我有request.getItems()。iterator()。next()。getId()== null,而它应该是1.如果我使用嵌套元素而不是属性,一切正常。 How it could be fixed?
如何解决? I dont'n want to write a batch of XmlAdapters for all primitive Java types.
我不想为所有原始Java类型编写一批XmlAdapter。
Attributes in XML are by default not qualified with the namespace of their parent element. 默认情况下,XML中的属性不使用其父元素的命名空间限定。 So for
因此对于
<item id="3" xmlns="foo"/>
The item
element has the namespace foo
, but the id
attribute does not. item
元素具有命名空间foo
,但id
属性没有。
To fix your problem, you should just need to remove the namespace
declaration from the getId()
method: 要解决您的问题,您只需要从
getId()
方法中删除namespace
声明:
@XmlAttribute(name = "id")
public Long getId() {
return _id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.