JAXB 编组到 XML 文件中的第一个空行

JAXB Marshalling to first blank line in XML file

我创建了一个程序,它根据预制模式将 XML 数据编组到我的项目文件夹中的 XML 文件。但是,我不知道如何修改程序以将数据编组到它遇到的第一个空白行,而不是覆盖文件中已有的数据。

这是我的主要代码 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
        }           
    }
}

我已尝试查看 JAXB 编组器选项,但没有找到适用于我的问题的选项。如果您能提供任何帮助,我们将不胜感激。

很抱歉在没有发布解决方案的情况下搁置了这么久。该模块刚刚结束,我正忙于完成它的工作。

我找到的解决方案是首先从 XML 中解组数据,将其存储在一个变量中,然后通过添加要添加到文件中的新数据来更新所述变量。从那里开始,这只是一个简单的用新变量重新编组的情况,然后 boom,添加数据而不覆盖以前的数据。