Search code examples
javaxmljaxbunmarshallingresttemplate

Java UnMarshall XML gives null objects


UnMarshalling XML gives null java objects.

XML :

<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item Name="John"/>
<Item Name="Jason"/>
</Items>

Items class :

@XmlRootElement(name = "Items")
@XmlAccessorType(XmlAccessType.FIELD)
public class Items{

    @XmlElement
    private List<Item> item; 

    public List<Item> getItem() {
    return this.Item;
}

}

Item class :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Item")
public class Item{

    @XmlAttribute
    private String name;

   public String getName() {
    return this.Name;
   }

}

Java Code that UnMarshalls : Here result.getBody gives XML String

ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);

 
        JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        
        Items itResult =  (Items) jaxbUnmarshaller.unmarshal(new StringReader(result.getBody()));

Item object always comes null. How to unmarshall the xml correctly ? Thanks in Advance. :


Solution

  • Use the following classes :

    Item.java

    @XmlRootElement(name = "Item")
    public class Item
    {
    
    
       private String name;
    
       public String getName() 
       {
          return this.name;
       }
    
       @XmlAttribute(name = "Name" )
       public void setName( String name )
       {
           this.name = name;
       }
    
    }
    

    Items.java

    @XmlRootElement(name = "Items")
    public class Items
    {
    
        @XmlElementWrapper( name = "Items")
        private List<Item> items = new ArrayList<Item>();
    
    
        public List<Item> getItemList() 
        {
           return this.items;
        }
    
        @XmlElement(name = "Item")
        public void setItemList( List<Item> items )
        {
            this.items = items;
        }
    
    }
    

    Test.java

    public class Test
    {
        public static void main( String... args )
        {
            try
            {
                JAXBContext jaxbContext = JAXBContext.newInstance( Items.class );
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                Items itResult =  (Items) jaxbUnmarshaller.unmarshal( new File( "Items.xml" ) );
    
                if ( itResult != null )
                {
                    List<Item> items = itResult.getItemList();
                    for ( Item item : items )
                    {
                        System.out.println( item.getName() );
                    }
                }
            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }
        }
    
    }
    

    You should get the Items object which contains Item s.

    Changes I made:

    a) You need an @XmlElementWrapper on the list which says that the Items is a wrapper around Items.

    b) Move the @XmlAttribute to setter in Item.java

    c) Move the @XmlElement to setter in Items.java