JAXB 编组 Java 以输出 XML 文件
2022-09-03 00:55:29
问题是我如何生成XML文件输出而不是system.out?
package jaxbintroduction;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
itemorder.Book quickXML = new itemorder.Book();
quickXML.setAuthor("Sillyme");
quickXML.setDescription("Dummie book");
quickXML.setISBN(123456789);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("Hello World JAVA");
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(quickXML, System.out);
OutputStream os = new FileOutputStream( "nosferatu.xml" );
marshaller.marshal( quickXML, os );
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
}
}
}