我有一个超级'车辆'如下所示,它提供了一系列子类的核心信息,其中一个子类是“Car'也显示如下。
package transport;
public abstract class RoadVehicle
{
public enum FuelType
{
ELECTRIC, PETROL, DIESEL
}
private final FuelType fuelType;
private String licencePlateRegistration;
private int ownerId;
private final short yearManufactured;
private final boolean isAutomatic;
private static int numberOfVehicles = 0;
public RoadVehicle(FuelType fuelType, String licencePlateRegistration, int ownerId, short yearManufactured, boolean isAutomatic)
{
this.fuelType = fuelType;
this.licencePlateRegistration = licencePlateRegistration;
this.ownerId = ownerId;
this.yearManufactured = yearManufactured;
this.isAutomatic = isAutomatic;
numberOfVehicles ++;
}
public FuelType getFuelType()
{
return this.fuelType;
}
public short getYearManufactured()
{
return this.yearManufactured;
}
public boolean getIsAutomatic()
{
return this.isAutomatic;
}
public void setLicencePlateNumber(String licencePlateNumber)
{
this.licencePlateRegistration = licencePlateNumber;
}
public String getLicencePlateNumber()
{
return this.licencePlateRegistration;
}
public void setOwnerId(int ownerId)
{
this.ownerId = ownerId;
}
public int getOwnerId()
{
return this.ownerId;
}
public int getNumberOfVehicles()
{
return RoadVehicle.numberOfVehicles;
}
}
汽车课
package transport;
public class Car extends RoadVehicle
{
private final short engineSizeInCC;
private static int numberOfCars = 0;
public Car(FuelType fuelType, String licencePlateRegistration, int ownerId, short yearManufactured, boolean isAutomatic, short engineSizeInCC)
{
super(fuelType, licencePlateRegistration, ownerId, yearManufactured, isAutomatic);
this.engineSizeInCC = engineSizeInCC;
numberOfCars ++;
}
public short getEngineSizeInCC()
{
return this.engineSizeInCC;
}
public int getNumberOfCars()
{
return Car.numberOfCars;
}
}
使用JaxB教程我试图提供一个XML文件(现在只包含一个汽车),以便应用程序使用以下类创建Java对象
package transport;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XMLParser
{
public static void main(String[] args)
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(Car.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
File XMLfile = new File("C:\\home\\XML.xml");
Car car = (Car) jaxbUnmarshaller.unmarshal(XMLfile);
System.out.println("CAR " +car.getLicencePlateNumber());
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
当我运行此命令时,我收到以下错误消息:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
transport.Car does not have a no-arg default constructor.
this problem is related to the following location:
at transport.Car
transport.RoadVehicle does not have a no-arg default constructor.
this problem is related to the following location:
at transport.RoadVehicle
at transport.Car
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:436)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at transport.XMLParser.main(XMLParser.java:15)
考虑到XML解析器类只创建一个Car对象,我会走在正确的道路上,而我需要解析器来解释XML应该转换为什么类型的对象,即van,car,lorry?