我在使用 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完全正确也很奇怪......