NULL values in JAXB unmarshalling

DunJen.coder :

HI I want to fetch the value of nested xml using un-marshaling using maven dependency but the final output is returning me null values.I have used the 3 packages in maven project and vehicle.xml contains the values of car after fetching the values I have to insert them into access Database

MY XML file---> Vehicle.xml

<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
   <Car>
      <manufacturer>Maruti</manufacturer>
      <cost>675000</cost>
      <name>Ciaz</name>
      <fueType>Petrol</fueType>
      <driverTye>Manual</driverTye>
   </Car>
   <Car>
      <manufacturer>Maruti</manufacturer>
      <cost>575000</cost>
      <name>Dezire</name>
      <fueType>Petrol</fueType>
      <driverTye>Manual</driverTye>
   </Car>
</Vehicle>

POJO CLASS

Vehicle.java

package jaxb;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Vehicle")
public class Vehicle {

    @XmlElement
    private List<Car> car;

    public List<Car> getCar() {
        return car;
    }

    /*
     * public Vehicle(List<Car> car) { super(); this.car = car; }
     */

    @Override
    public String toString() {
        return "Vehicle[ Car="+car+"]";
    }

}

Car.java (This is child POJO)

package jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Car")
public class Car {

    private String manufacturer;
    private String name;
    private String driverType;
    private String fuelType;
    private int cost;

    @XmlElement
    public String getManufacturer() {
        return manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getDriverType() {
        return driverType;
    }
    public void setDriverType(String driverType) {
        this.driverType = driverType;
    }

    @XmlElement
    public String getFuelType() {
        return fuelType;
    }
    public void setFuelType(String fuelType) {
        this.fuelType = fuelType;
    }

    @XmlElement
    public int getCost() {
        return cost;
    }
    public void setCost(int cost) {
        this.cost = cost;
    }


     @Override
        public String toString() {
            return "Car [name=" + name + ", fuelType=" + fuelType + ", cost=" + cost+",driverType="+driverType +"]";
        }

VehicleJxb.java

This file contains the implementation of our unmarshalling method

package jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class VehicleJxb {



    public void unmarhalling() {


        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Vehicle.class);  

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  

           Vehicle  vehicle = (Vehicle) jaxbUnmarshaller.unmarshal(new File("src\\main\\java\\Data\\Vehicle.xml"));
             System.out.println(vehicle);  
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  



    }

}

App.java

package com.project.XMLDB;
import jaxb.*;
public class App 
{
    public static void main( String[] args ) 
    {
        VehicleJxb obj= new VehicleJxb();
       obj.unmarhalling();
    }
}

My Output is coming

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/Shivam%20Sharma/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-impl-2.2.11.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Vehicle[ Car=null]

I want to get the values as final output is returned null

Pasupathi Rajamanickam :

Unmarshaller is case sensitive. In your pojo, you have variable name car but in XML you Car. Change it to the following and it will work.

<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
   <car>
      <manufacturer>Maruti</manufacturer>
      <cost>675000</cost>
      <name>Ciaz</name>
      <fueType>Petrol</fueType>
      <driverTye>Manual</driverTye>
   </car>
   <car>
      <manufacturer>Maruti</manufacturer>
      <cost>575000</cost>
      <name>Dezire</name>
      <fueType>Petrol</fueType>
      <driverTye>Manual</driverTye>
   </car>
</Vehicle>

Or you need to mention that explicitly.

@XmlElement(name = "Car")
private List<Car> car;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326681&siteId=1