Search code examples
javaxmljaxbmarshalling

JAXB Marshalling to first blank line in XML file


I've created a program that marshalls XML data to an XML file in my project folder based on a premade schema. However, I am stuck as to how I can modify the program to marshall the data to the first blank line it comes across instead of overwriting the data already in the file.

This is the code for my main Java class:

//Client Application that users can see and interact with. 

package shares_system_client_application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;    
import java.time.ZoneId;
import java.util.Date;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Shares_System_Client_Application
{
    public static void main(String[] args) throws FileNotFoundException, IOException 
    {
        //Creates instance of binded XML file.
        //Shares_Info = Package name
        //CurrentShares = bound XML file
        Shares_Info.CurrentShares quickXML = new Shares_Info.CurrentShares();

        quickXML.setCompanyName("test co");
        quickXML.setCompanySymbol("DOLLAR");

        Date now = new Date();
        Instant current = now.toInstant();
        LocalDateTime ldt = LocalDateTime.ofInstant(current, ZoneId.systemDefault());

        String dateTimeString = ldt.toString();

        try
        {
            XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTimeString);

            quickXML.setLastShareUpdate(date2);

        }

        catch (Exception e)
        {
             System.out.println("Oopsie Poopsie");
        }

        quickXML.setNumOfShares(43000);

        Shares_Info.CurrentShares.SharePrice quickXML2 = new Shares_Info.CurrentShares.SharePrice();
        quickXML2.setCurrency("Dollar");

        quickXML2.setValue(123.4f);

        quickXML.setSharePrice(quickXML2);


        //Marshelling code
        try 
        {   
            //The JAXBContext class provides the client's entry point to the JAXB API
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());

            //The Marshaller class is responsible for governing the process of 
            //serializing Java content trees back into XML data
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();

            //Specified encoding
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N

            //The name of the property used to specify whether or not the 
            //marshalled XML data is formatted with linefeeds and indentation.
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);          

            File Shares_File = new File("Shares_Data.xml");

            marshaller.marshal(quickXML, Shares_File);
        }

        catch (javax.xml.bind.JAXBException ex) 
        {            
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        }           
    }
}

I've tried looking at the JAXB marshaller options and can't see one that applies to my problem. Any help you can provide would be greatly appreciated.


Solution

  • Apologies for leaving this up for so long without posting the solution. The module has only just finished and I was busy with completing the work for it.

    The solution I found for this was to unmarshal the data from the XML first, store it in a variable and then update said variable by adding the new data that you want to add to the file. From there, it's just a simple case of re-marshalling with the new variable and then boom, the data is added without overwriting the previous data.