【问题标题】:JAXB Marshalling to first blank line in XML fileJAXB 编组到 XML 文件中的第一个空行
【发布时间】:2019-12-31 15:08:16
【问题描述】:

我创建了一个程序,该程序根据预制架构将 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 编组器选项,但找不到适用于我的问题的选项。您能提供的任何帮助将不胜感激。

【问题讨论】:

    标签: java xml jaxb marshalling


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 2023-03-23
      • 1970-01-01
      • 2012-11-27
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多