【发布时间】:2018-05-22 07:13:29
【问题描述】:
你好,我正在尝试将 xml 与对象数量进行转换,我得到了一个 错误消息:文档中根元素之后的标记必须格式正确。
XML:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="test.example.com">
<Item>
<ItemKey>1111</ItemKey>
<Start>2/10/2017</Start>
<customNumber>12</customNumber>
<End>2/10/2018</End>
<Account>2221111</Account>
<Name>John</Name>
<Note>GOOD</Note>
<CodeNo>4444-1</CodeNo>
<Source>www.cnn.com</Source>
</Item>
<Item>
<ItemKey>2222</ItemKey>
<Start>2/10/2017</Start>
<customNumber>75</customNumber>
<End>2/10/2018</End>
<Account>3333111</Account>
<Name>Smith</Name>
<Note>NOT GOOD</Note>
<CodeNo>4444-2</CodeNo>
<Source>www.fox.com</Source>
</Item>
</string>
模型类:
package example.models;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Item")
public class Model {
private String CodeNo;
private String ItemKey;
private String Start;
private String End;
private String Account;
private String Name;
private String Note;
...(gets and sets)
主要代码:
StringReader reader = new StringReader(response);
String response = restTemplate.getForObject(url, String.class);
...
JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Model recordes = (Model) unmarshaller.unmarshal(reader);
解组异常: 文档中根元素之后的标记必须格式正确。
xml 只有一项代码可以工作。
我缺少什么并且需要做什么才能正确获取元素(项目)对象列表?
【问题讨论】:
标签: java xml object spring-boot