【发布时间】:2017-06-08 12:56:31
【问题描述】:
UnMarshalling XML 给出空 java 对象。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item Name="John"/>
<Item Name="Jason"/>
</Items>
物品类:
@XmlRootElement(name = "Items")
@XmlAccessorType(XmlAccessType.FIELD)
public class Items{
@XmlElement
private List<Item> item;
public List<Item> getItem() {
return this.Item;
}
}
物品类别:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Item")
public class Item{
@XmlAttribute
private String name;
public String getName() {
return this.Name;
}
}
UnMarshalls 的 Java 代码:这里 result.getBody 给出 XML 字符串
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Items itResult = (Items) jaxbUnmarshaller.unmarshal(new StringReader(result.getBody()));
Item 对象总是为空。如何正确解组 xml? 提前致谢。 :
【问题讨论】:
-
你如何使用这些类?
-
真正解组整个事情的代码在哪里?
-
Item不是XmlRootElement恕我直言。 -
是的,这个 xml 是不同的。它不包含像
. 这样的简单标签 -
@SarweshSethiya 你想说什么?很明显
name是一个属性。
标签: java xml jaxb unmarshalling resttemplate