Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how convert xml to object on java spring-boot

Tags:

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 ?

like image 278
Mr K Avatar asked May 22 '18 07:05

Mr K


People also ask

Can we convert XML to JSON in Java?

We can convert XML to JSON array using org. json. XML class, this provides a static method, XML. toJSONObject() to convert XML to JSON array.

What is the JAXB process for converting an XML document to Java object called?

Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.

Can we convert XML to JSON?

To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.


1 Answers

In the XML file your root element is <string xmlns="test.example.com"> tag so either correct the XML or correct the Model class to get it work.

To understand more about the error check this How to fix error: The markup in the document following the root element must be well-formed

edit

You can use this tool to generate the POJO : http://pojo.sodhanalibrary.com

here are the POJO classes for above XML:

public class MyXML
{
    private String string; // Change the class as String is Wrapper class

    public String getString ()
    {
        return string;
    }

    public void setString (String string)
    {
        this.string = string;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [string = "+string+"]";
    }
}

String class:

public class String // Change this className as String is Wrapper class in java
{
    private Item[] Item;

    private String xmlns;

    public Item[] getItem ()
    {
        return Item;
    }

    public void setItem (Item[] Item)
    {
        this.Item = Item;
    }

    public String getXmlns ()
    {
        return xmlns;
    }

    public void setXmlns (String xmlns)
    {
        this.xmlns = xmlns;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Item = "+Item+", xmlns = "+xmlns+"]";
    }
}

Items class

public class Item
{
    private String Name;

    private String Source;

    private String End;

    private String CodeNo;

    private String Start;

    private String Account;

    private String ItemKey;

    private String Note;

    private String customNumber;

    public String getName ()
    {
        return Name;
    }

    public void setName (String Name)
    {
        this.Name = Name;
    }

    public String getSource ()
    {
        return Source;
    }

    public void setSource (String Source)
    {
        this.Source = Source;
    }

    public String getEnd ()
    {
        return End;
    }

    public void setEnd (String End)
    {
        this.End = End;
    }

    public String getCodeNo ()
    {
        return CodeNo;
    }

    public void setCodeNo (String CodeNo)
    {
        this.CodeNo = CodeNo;
    }

    public String getStart ()
    {
        return Start;
    }

    public void setStart (String Start)
    {
        this.Start = Start;
    }

    public String getAccount ()
    {
        return Account;
    }

    public void setAccount (String Account)
    {
        this.Account = Account;
    }

    public String getItemKey ()
    {
        return ItemKey;
    }

    public void setItemKey (String ItemKey)
    {
        this.ItemKey = ItemKey;
    }

    public String getNote ()
    {
        return Note;
    }

    public void setNote (String Note)
    {
        this.Note = Note;
    }

    public String getCustomNumber ()
    {
        return customNumber;
    }

    public void setCustomNumber (String customNumber)
    {
        this.customNumber = customNumber;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Name = "+Name+", Source = "+Source+", End = "+End+", CodeNo = "+CodeNo+", Start = "+Start+", Account = "+Account+", ItemKey = "+ItemKey+", Note = "+Note+", customNumber = "+customNumber+"]";
    }
}
like image 81
smali Avatar answered Sep 28 '22 18:09

smali