Home » Java » how convert xml to object on java spring-boot

how convert xml to object on java spring-boot

Posted by: admin June 30, 2018 Leave a comment

Questions:

hello I’m trying to convert xml with number of objects and I get an
error message : The markup in the document following the root element must be well-formed.

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>

Model Class:

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)

main Code:

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);

unmarshal exception:
The markup in the document following the root element must be well-formed.

xml with only one item the code work.

what I missing and need to do to get list of element (items) object without error ?

Answers: