How to make an abstract class work with JAXB

Dear fellow programmers, I used an example from http://www.vogella.com/articles/JAXB/article.html

to use JAXB XML for my 3 classes, UserStorage, User and UserTest

it works fine, but it's just unmarchialing

JAXBContext context = JAXBContext.newInstance(UserStorage.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); the User class is ABSTRACT!, so it throws an 

Exception in the "main" thread javax.xml.bind.UnmarshalException: Unable to create an instance of platform.User - with the associated exception: [java.lang.InstantiationException] in com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext .handleEvent (UnmarshallingContext.java:648) in com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError (Loader.java:236) in com.sun.xml.internal.bind.v2.runtime .unmarshaller.UnmarshallingContext.createInstance (UnmarshallingContext.java:615) in com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement (StructureLoader.java:170) in com.sun.xml.internal .v2.runtime.unmarshaller.UnmarshallingContext._startElement (UnmarshallingContext.java:487) in com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement (UnmarshallingContext.javaunx65) .internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement (SAXConnector.java : 135) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement (AbstractSAXParser.java∗01) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement (XMLNSDocumentImpl : 400) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next (XMLDocumentFragmentScannerImpl.java:2756) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next .java: 648) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next (XMLNSDocumentScannerImpl.java:140) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocumentragment (XMLDocument .java: 511) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse (XML11Configuration.java:808) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse (XML11Configuration .java: 737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse (XMLPa rser.java:119) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse (AbstractSAXParser.java:1205) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser. parse (SAXParserImpl.java∗22) in com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0 (UnmarshallerImpl.java:200) in com.sun.xml.internal.bind.v2.runtime. unmarshaller.UnmarshallerImpl.unmarshal (UnmarshallerImpl.java:173) in javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal (AbstractUnmarshallerImpl.java:137) in javax.xml.bind.helpers.AbstractUnmarshal on the .UserTest.main platform (UserTest.java:77) Raised: java.lang.InstantiationException at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance (InstantiationExceptionConstructorAccessorImpl.java:30) in java.lang.reflect.Cstructor.constructor.constructor.constructor 513) in com.sun.xml.internal.bind.v2.ClassFactory.create0 (ClassFactory.java:112) in com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance (ClassBeanInfoImpl.java:231) in com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext .createInstance (UnmarshallingContext.java:609) ... 20 more

Is there any solution for this, so I could associate the User class with XML, since I can save the XML file with the user's details, but when I want to get it, it adds that the user class is abstract, I have an admin , broker, child subclasses, but so far in my test user class I have created only 4 admins for testing, thanks and hope you can help.

 import platform.UserStorage; import platform.User; public class UserTest { private static final String USER_XML = "user2.xml"; public static void main(String[] args) throws JAXBException, IOException { ArrayList<User> userList = new ArrayList<User>(); // create test users User user1 = new Admin(); user1.setName("Dave"); user1.setPass("1234"); user1.setDeleted(true); user1.setBan(false); userList.add(user1); User user2 = new Admin(); user2.setName("James"); user2.setPass("1234"); user2.setDeleted(true); user2.setBan(false); userList.add(user2); User user3 = new Admin(); user3.setName("Mike"); user3.setPass("1234"); user3.setDeleted(true); user3.setBan(false); userList.add(user3); // create bookstore, assigning book UserStorage userstore = new UserStorage(); userstore.setListName("Test List"); userstore.setUserList(userList); // create JAXB context and instantiate marshaller JAXBContext context = JAXBContext.newInstance(UserStorage.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(userstore, System.out); Writer w = null; try { w = new FileWriter(USER_XML); m.marshal(userstore, w); } finally { try { w.close(); } catch (Exception e) { } } // get variables from our xml file, created before System.out.println(); System.out.println("Output from our XML File: "); Unmarshaller um = context.createUnmarshaller(); UserStorage userstore2 = (UserStorage) um.unmarshal(new FileReader( USER_XML)); for (int i = 0; i < userstore2.getUsersList().toArray().length; i++) { System.out.println("User " + (i + 1) + ": " + userstore2.getUsersList().get(i).getName() + " Pass " + userstore2.getUsersList().get(i).getPass()); }} } package platform; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; //If you want you can define the order in which the fields are written //Optional /** * @author dinesh.kaushish, james.wyche //updated XML properties. * */ @XmlRootElement(name = "user") @XmlType(propOrder = { "name", "pass", "deleted", "ban" }) 

..

 public abstract class User implements UserInterface { private String name; private String pass; private boolean deleted; private boolean ban; /** * @ author dinesh.kaushish * @param String username * return void */ public void setName(String name) { this.name = name; } // If you like the variable name, eg "name", you can easily change this // name for your XML-Output: /** * @author dinesh.kaushish * @param null * @return String user; */ @XmlElement(name = "user") public String getName() { return this.name; } /** * @author dinesh.kaushish * @param String pwd * @return void */ public void setPass(String pass) { this.pass=pass; } /** * @author dinesh.kaushish * @param void * @return String password */ @XmlElement(name = "pass") public String getPass() { return pass; } /** * @author dinesh.kaushish * @param dFlag * @return void */ public void setDeleted(boolean deleted) { this.deleted = deleted; } /** * @author dinesh.kaushish * @return boolean isDeleted */ @XmlElement(name = "deleted") public boolean getDeleted() { return deleted; } /** * @author dinesh.kaushish * @param bFlag */ public void setBan(boolean ban) { this.ban = ban; } /** * @author dinesh.kaushish * @return Boolean isBanned */ @XmlElement(name = "ban") public Boolean getBan() { return ban; } public abstract void addUser(); public abstract void removeUser(); public abstract void verifyUser(); public abstract void passReset(); public abstract void faultReport(); public abstract void RequestsForAccess(); public abstract void UpdateDetails(); public abstract void BanUser(); public abstract void ChangePermissions(); } package platform; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.HashMap; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; import java.util.ArrayList; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; /** * @author michael.wambeek, james.wyche //added JAXB support. * */ //This statement means that class "Bookstore.java" is the root-element of our example @XmlRootElement(namespace = "platform") public class UserStorage { // XmLElementWrapper generates a wrapper element around XML representation @XmlElementWrapper(name = "userList") // XmlElement sets the name of the entities @XmlElement(name = "user") private ArrayList<User> userList; private String listName = ""; 

// closed static instance UserStorage; // end james.wyche JAXB codes.

 public UserStorage(){ } /** * @author michael.wambeek * * Searches for a username and returns the password. * * @param username The username to search for * @return The password of the correct username or null * @throws Exception */ public String findUser(String username) throws Exception{ return search(username); } public boolean storeUser(String username, String password, UserType type){ return true; } /** * @author james.wyche * @param userList */ public void setUserList(ArrayList<User> userList) { this.userList = userList; } /** * * @return UserList */ public ArrayList<User> getUsersList() { return userList; } 
+6
source share
4 answers

Why is this happening because Jaxb will try to create a user instance. which is abstract and therefore failure.

Add annotations in your abstract class

 @XmlTransient //Prevents the mapping of a JavaBean property/type to XML representation @XmlSeeAlso({Admin.class, <other class>}) //Instructs JAXB to also bind other classes when binding this class 

see javadoc for each ( XmlTransient , XmlSeeAlso )

What this will do is to prevent jaxb from trying to initialize the abstract class.

The only drawback of this method that I found is that it adds extra namespace information to the generated xml object.

+9
source

You need to add the XmlSeeAlso annotation to the User class with Admin attributes and all other specific classes that subclass the User class.

 @XmlSeeAlso({Admin.class}) 

PS, do not forget to add the @XmlRootElement Xml tag to the Admin class.

+6
source

This will not work because JAXB should create new instances (objects) of your classes when unmarshalling xml. And if the tag from xml is associated with an abstract class, it simply cannot create an instance of an object from this class. You either need to make the user class non-abstract, or bind the xml tag to a specific user subclass.

0
source

You must specify a concrete type for each element:

 <user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Admin"> ... </user> 
0
source

Source: https://habr.com/ru/post/913842/


All Articles