以下のコードが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 ファイルにまとめたいのですが、ヒントはありますか?