你好,我正在尝试转换具有多个对象的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中只有一个项目时,代码可以工作。
我漏掉了什么并需要做什么才能获得元素(项)对象列表而不出现错误?