簡體   English   中英

循環jaxb馬歇爾

[英]loop a jaxb marshall

我只想循環使用相同xml元素但具有不同ID的jaxb marshal,不像我下面的代碼解析每個ID的新xml文件並寫入舊ID

data [] []從矩陣中讀入,data [i] [0]表示ID列表,我想將這些id設置為客戶ID。

      Customer customer = new Customer();
      File file = new File("C:/data.xml");

      for (int i = 0; i < data.length; i++) {
          customer.setId(data[i][0]);

            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(customer, file);
            jaxbMarshaller.marshal(customer, System.out);


      }

上面代碼的輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="2"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="3"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="4"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="5"/>

我想在一個單獨的xml文件中輸出所有ID,任何提示?

您可以在Marshaller上設置JAXB_FRAGMENT屬性,以防止寫出標頭。 在編組到另一個文檔時,這是必要的。

package forum12925616;

import java.io.*;
import javax.xml.bind.*;

public class Demo {

    private static int X = 5;
    private static int Y = 2;

    public static void main(String[] args) throws Exception {
        int[][] data = new int[X][Y];
        for (int x = 0; x < X; x++) {
            for (int y = 0; y < Y; y++) {
                data[x][y] = x;
            }
        }

        // Create this once
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        marshal(jaxbMarshaller, data, System.out);

        FileOutputStream fileOutputStream = new FileOutputStream("src/forum12925616/out.xml");
        marshal(jaxbMarshaller, data, fileOutputStream);
        fileOutputStream.close();
    }

    private static void marshal(Marshaller jaxbMarshaller, int[][] data, OutputStream outputStream) throws Exception {
        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
        writer.write("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        writer.write("\n<customers>\n");
        for (int i = 0; i < data.length; i++) {
            Customer customer = new Customer();
            customer.setId(data[i][0]);
            jaxbMarshaller.marshal(customer, writer);
            writer.write("\n");
        }
        writer.write("<customers>");
        writer.flush();
    }

}

系統和文件輸出

下面是控制台和文件的結果輸出。

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<customers>
<customer id="0"/>
<customer id="1"/>
<customer id="2"/>
<customer id="3"/>
<customer id="4"/>
<customers>

欲獲得更多信息

只需將marhsaller的構造移出循環。 您可以在同一個線程中多次重用marshaller。

或者您是否真的想要將客戶列表輸出為單個xml文件? 然后,您需要一個Customer對象數組,然后對陣列進行編組。

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM