1

以下のコードがIDごとに新しいxmlファイルを解析し、古いものを上書きするのとは異なり、同じxml要素で異なるIDを持つjaxbマーシャルをループしたいだけです。

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"/>

すべての ID出力を 1 つの xml ファイルにまとめたいのですが、ヒントはありますか?

4

2 に答える 2

3

JAXB_FRAGMENTのプロパティを設定してMarshaller、ヘッダーが書き出されないようにすることができます。これは、別のドキュメントにマーシャリングするときに必要です。

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>

詳細については

于 2012-10-17T09:34:16.563 に答える
1

マーサラーの構造をループの外に移動するだけです。同じスレッドでマーシャラーを何度でも再利用できます。

それとも、顧客のリストを単一のxmlファイルとして出力しようとしていますか?次に、Customerオブジェクトの配列が必要になり、その配列をマーシャリングします。

于 2012-10-17T02:15:06.067 に答える