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

问题描述 投票:0回答:1

我已经创建了一个程序,该程序基于预设的架构将XML数据编组到我的项目文件夹中的XML文件中。但是,对于如何修改程序以将数据编组到遇到的第一个空白行而不是覆盖文件中已有的数据,我深感困惑。

这是我的主要Java类的代码:

//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 marshaller选项,但看不到适用于我的问题的选项。您可以提供的任何帮助将不胜感激。

java xml jaxb marshalling
1个回答
0
投票

很抱歉在不发布解决方案的情况下搁置了这么长时间。该模块才刚刚完成,我正忙于完成它的工作。

我为此找到的解决方案是先从XML解组数据,将其存储在变量中,然后通过将要添加的新数据添加到文件中来更新所述变量。从那里开始,这只是使用新变量重新编组然后进行繁荣的简单情况,在不覆盖先前数据的情况下添加了数据。

© www.soinside.com 2019 - 2024. All rights reserved.