6

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 ?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Mr K
  • 61
  • 1
  • 1
  • 4

4 Answers4

4

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+"]";
    }
}
smali
  • 4,687
  • 7
  • 38
  • 60
  • how I need to add xmlns to spring boot model ? – Mr K May 22 '18 at 07:25
  • @MrK, Edited my answer is that you were looking or something else? – smali May 22 '18 at 08:45
  • ok and how I need combine this with : JAXBContext jaxbContext = JAXBContext.newInstance(Model.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Model recordes = (Model) unmarshaller.unmarshal(reader); – Mr K May 22 '18 at 09:10
3

Step by step process for creating XML to object in Springboot

  1. First, you need to create maven project give Group id and Artifact id.

2.Create your java class in src/main/java and your package name and your java class name for Example-XmlToJavaObject.java

3.Convert your XML code into POJO class using below link

http://pojo.sodhanalibrary.com/Convert

It will create POJO class according to your XML data. Create a package for POJO and put All POJO classes into this folder.

  1. Put your XML file in this project near pom.xml.

  2. Create main method into XmlToJavaObject.java class.

I am Writing below code but you can change it according to your requirements.

Our Example XML is

<?xml version="1.0" encoding="UTF-8"?>
      <Student>
        <id>1111</id>
        <name>Ravi</name>
        <age>12</age>
        <dob>2/10/2008</dob>
    </Student>

Our Model class

import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
        public class Student
        {
            private String dob;

            private String name;

            private String id;

            private String age;

            public String getDob ()
            {
                return dob;
            }

            public void setDob (String dob)
            {
                this.dob = dob;
            }

            public String getName ()
            {
                return name;
            }

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

            public String getId ()
            {
                return id;
            }

            public void setId (String id)
            {
                this.id = id;
            }

            public String getAge ()
            {
                return age;
            }

            public void setAge (String age)
            {
                this.age = age;
            }

            @Override
            public String toString()
            {
                return "ClassPojo [dob = "+dob+", name = "+name+", id = "+id+", age = "+age+"]";
            }
        }

Our Main Class Example

import java.io.File;
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Unmarshaller;

import com.test.pojo.Student;


public class XmlToJavaObject {


    public static void main(String ar[])
    {
         try {  

                File file = new File("Student.xml");  
                JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);  

                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
                Student student= (Student) jaxbUnmarshaller.unmarshal(file);  


                System.out.println(student.getName());


              } catch (JAXBException e) {  
                e.printStackTrace();  
              }
    }
}

Output

Ravi
Ravikant
  • 149
  • 1
  • 5
-1
int PRETTY_PRINT_INDENT_FACTOR = 4;
String TEST_XML_STRING =
        "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to 
JSON</test>";

try {
     JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
     String jsonPrettyPrintString = 
xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
     System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
     System.out.println(je.toString());
}

hope this will help you, use org.json jar file

Gokul raj
  • 53
  • 5