JAXB を使用して Java オブジェクトを XML ファイルに書き込む際に問題が発生します。
私の方法は次のようになります。
public void printToXml(PNLExport export, String outputPath, boolean syso)throws Exception
{
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputPath);
JAXBContext contxt = JAXBContext.newInstance(PNLExport.class);
Marshaller m = contxt.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
if(syso){
System.out.println();
m.marshal(export, System.out);
}
m.marshal(export, fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}//printToXml
ルート要素タグを閉じた後、いくつかの奇妙な動作が表示されます:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PNLExport version="">
<Header>
<RecType>0</RecType>
<DateFormat>DD.MM.YY</DateFormat>
<TimeFormat>HH:MM</TimeFormat>
<TimeMode>L</TimeMode>
<GenDate>25.06.12</GenDate>
<GenTime>09:45</GenTime>
</Header>
<Records>
<Record>
<FlightRecord>
<RecType>21</RecType>
<Carrier>HG</Carrier>
<FlightNumber>8332</FlightNumber>
<FlightDate>30.06.12</FlightDate>
<Departure>VIE</Departure>
<Destination>OLB</Destination>
<DepTime>09:40</DepTime>
<DesTime>11:30</DesTime>
</FlightRecord>
<PaxRecord>
<RecType>32</RecType>
<BookingNumber>11632</BookingNumber>
<PaxNumber>1</PaxNumber>
<Name>SCHABAUER,Franz</Name>
<Salutation>MR</Salutation>
<BookingState>OK</BookingState>
<TicketType>T</TicketType>
</PaxRecord>
<PaxRecord>
<RecType>32</RecType>
<BookingNumber>11632</BookingNumber>
<PaxNumber>2</PaxNumber>
<Name>SCHABAUER,Vera</Name>
<Salutation>MRS</Salutation>
<BookingState>OK</BookingState>
<TicketType>T</TicketType>
</PaxRecord>
</Record>
</Records>
.
.
.
</PNLExport>
rrier>
<FlightNumber>8332</FlightNumber>
<FlightDate>02.07.12</FlightDate>
<Departure>VIE</Departure>
<Destination>OLB</Destination>
<DepTime>09:15</DepTime>
<DesTime>10:55</DesTime>
</FlightRecord>
<FlightRecord>
<RecType>21</RecType>
<Carrier>HG</Carrier>
<FlightNumber>8333</FlightNumber>
<FlightDate>02.07.12</FlightDate>
<Departure>OLB</Departure>
<Destination>VIE</Destination>
<DepTime>11:40</DepTime>
<DesTime>13:20</DesTime>
</FlightRecord>
<FlightRecord>
<RecType>21</RecType>
<Carrier>HG</Carrier>
<FlightNumber>8333</FlightNumber>
<FlightDate>29.06.12</FlightDate>
<Departure>OLB</Departure>
<Destination>VIE</Destination>
<DepTime>14:00</DepTime>
<DesTime>15:40</DesTime>
</FlightRecord>
</Record>
</Records>
</PNLExport>
ここで何がうまくいかないのですか?
時々xmlが完全に正しいことも奇妙です...