简体   繁体   English

JAXB 解组中的 NULL 值

[英]NULL values in JAXB unmarshalling

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 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必须将它们插入访问数据库

MY XML file---> Vehicle.xml MY XML 文件---> 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 POJO CLASS

Vehicle.java车辆.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) Car.java(这是孩子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车辆Jxb.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 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我的 Output 来了

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我想得到最终的值 output 返回 null

Unmarshaller is case sensitive. Unmarshaller区分大小写。 In your pojo, you have variable name car but in XML you Car .在你的 pojo 中,你有变量名car但在 XML 你有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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM