Skip to content
Andreas Rudolph edited this page Aug 15, 2021 · 19 revisions

Usage of XML processing

The following page shows some examples how to deal with XML and JAXB classes.

Writing a JAXB object into XML

The following example class creates a random contact person and real estate and writes its data into a temporary XML file.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.IOUtils;
import org.openestate.is24.restapi.utils.RandomRealEstateFactory;
import org.openestate.is24.restapi.utils.XmlUtils;
import org.openestate.is24.restapi.xml.common.RealtorContactDetails;
import org.openestate.is24.restapi.xml.realestates.RealEstate;

public class XmlWritingExample
{
  private final static String ENCODING = "UTF-8";
  private final static boolean PRETTY_PRINT = true;

  public static void main( String[] args )
  {
    final RandomRealEstateFactory factory = new RandomRealEstateFactory();
    OutputStream output = null;

    // writing an example contact person
    try
    {
      // generate random contact person
      RealtorContactDetails contact = factory.createRandomContact();

      // write contact person into a temporary file
      File file = File.createTempFile( "example-contact-", ".xml" );
      output = new FileOutputStream( file );
      XmlUtils.writeXml( contact, ENCODING, PRETTY_PRINT, output );
    }
    catch (JAXBException ex)
    {
      throw new RuntimeException( "Can't build XML file!", ex );
    }
    catch (IOException ex)
    {
      throw new RuntimeException( "Can't write XML file!", ex );
    }
    finally
    {
      IOUtils.closeQuietly( output );
    }

    // writing an example real estate
    try
    {
      // generate random real estate
      RealEstate object = factory.createRandomObject();

      // write real estate into a temporary file
      File file = File.createTempFile( "example-object-", ".xml" );
      output = new FileOutputStream( file );
      XmlUtils.writeXml( object, ENCODING, PRETTY_PRINT, output );
    }
    catch (JAXBException ex)
    {
      throw new RuntimeException( "Can't build XML file!", ex );
    }
    catch (IOException ex)
    {
      throw new RuntimeException( "Can't write XML file!", ex );
    }
    finally
    {
      IOUtils.closeQuietly( output );
    }
  }
}

(extracted from XmlWritingExample.java)

Reading XML into a JAXB object

The following example class takes one or more XML files from the command line arguments, parses the provided XML files into JAXB objects and writes the JAXB objects back into a XML string, that is finally written to the console.

import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.lang3.ArrayUtils;
import org.openestate.is24.restapi.utils.XmlUtils;
import org.openestate.is24.restapi.xml.realestates.RealEstate;

public class XmlReadingExample
{
  private final static String ENCODING = "UTF-8";
  private final static boolean PRETTY_PRINT = true;

  public static void main( String[] args )
  {
    if (ArrayUtils.isEmpty( args ))
    {
      System.out.println( "No files were specified for reading." );
    }

    // create an unmarshaller
    Unmarshaller unmarshaller = null;
    try
    {
      unmarshaller = XmlUtils.createUnmarshaller();
    }
    catch (JAXBException ex)
    {
      throw new RuntimeException( "Can't build unmarshaller!", ex );
    }

    for (String arg : args)
    {
      // get path of the XML file from command line arguments
      File file = new File( arg );
      if (!file.isFile())
      {
        System.out.println( "The provided file '" + arg + "' does not exist!" );
        continue;
      }

      // read XML at the provided location into a JAXB object
      RealEstate object = null;
      try
      {
        JAXBElement<RealEstate> xml = (JAXBElement<RealEstate>)
          unmarshaller.unmarshal( file );
        object = xml.getValue();
      }
      catch (JAXBException ex)
      {
        throw new RuntimeException(
          "Can't read XML file '" + file.getAbsolutePath() + "'!", ex );
      }
      if (object==null)
      {
        throw new RuntimeException(
          "Can't read XML file '" + file.getAbsolutePath() + "'!" );
      }

      // write the parsed JAXB object back into a XML string
      try
      {
        String xml = XmlUtils.marshal( object, ENCODING, PRETTY_PRINT );
        System.out.println( "-----------------------------------" );
        System.out.println( "processed content of " + file.getAbsolutePath() );
        System.out.println( xml );
      }
      catch (JAXBException ex)
      {
        throw new RuntimeException(
          "Can't process XML file '" + file.getAbsolutePath() + "'!", ex );
      }
      catch (IOException ex)
      {
        throw new RuntimeException(
          "Can't process XML file '" + file.getAbsolutePath() + "'!", ex );
      }
    }
  }
}

(extracted from XmlReadingExample.java)