Java – Unmarshaller using JAXB binding

The generated package has been generated by JAXB and the classes where generated too.

/**
* This method open, read and unmarshall a file
* @param URLFichier url of the file to open
* @return an entity object
*/
public MyEntity readFile (String URLfile){

 try {
   JAXBContext jc = JAXBContext.newInstance("generated"); //this is the name of the generated package
   Unmarshaller unmarshaller = jc.createUnmarshaller();
 
   File fileXML = new File(URLfile);
   JAXBElement parseResult = (JAXBElement) unmarshaller.unmarshal(fileXML);
   MyEntity myEntity = (MyEntity) parseResult.getValue();
 } 
 catch (JAXBException ex) {
   System.out.println("Erreur JAXB : " + ex.getMessage());
 } 
 catch (ClassCastException ex) {
   System.out.println("Erreur lors du cast : " + ex.getMessage());
 }
}
Advertisements

2 thoughts on “Java – Unmarshaller using JAXB binding

  1. Hi

    We use must the same – except we do not limit the method to specific types – rather introduce generics to allow for reuse. The other side of the coin is the save method – the only requirement is that the object type is the same across both methods:

    /**
    * Saves an entity instance to file.
    *
    * @param The entity type
    * @param fo The file object representing the entity instance
    * @param entity The entity to save
    * @throws IOException thrown if the entity save fails.
    */
    @SuppressWarnings(“unchecked”)
    public static void save(FileObject fo, T entity) throws IOException {
    assert fo != null;
    assert entity != null;
    FileLock lock = null;
    try {
    lock = fo.lock();
    DataUtility.save(FileUtil.toFile(fo), entity);
    } catch (Throwable t) {
    Exceptions.printStackTrace(t);
    throw new IOException(“Unable to save entity”, t);
    } finally {
    if (lock != null) {
    lock.releaseLock();
    }
    }
    }

    /**
    * Loads an entity instance from local storage
    *
    * @param The entity type
    * @param fo The file object representing the entity instance
    * @return The entity to load from disc
    * @throws IOException thrown if the entity load fails.
    */
    @SuppressWarnings(“unchecked”)
    public static T load(FileObject fo, Class cls) throws IOException {
    assert fo != null;
    FileLock lock = null;
    T entity = null;
    try {
    lock = fo.lock();
    JAXBContext context = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement root = unmarshaller.unmarshal(new StreamSource(FileUtil.toFile(fo)), cls);
    entity = root.getValue();
    } catch (IOException | JAXBException ex) {
    throw new IOException(“Unable to load entity”, ex);
    } finally {
    if (lock != null) {
    lock.releaseLock();
    }
    }
    if (entity == null) {
    throw new IOException(“Could not load the entity. Value is null.”);
    }
    return entity;
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s