- importjava.io.FileOutputStream; @H_301_12@
- importjavax.xml.bind.JAXBContext;
- importjavax.xml.bind.Marshaller;
- importjavax.xml.bind.annotation.XmlRootElement;
- publicclassJavaToXMLDemo{
- staticvoidmain(String[]args)throwsException{ @H_301_12@ JAXBContextcontext=JAXBContext.newInstance(Employee.class); @H_301_12@ Marshallerm=context.createMarshaller(); @H_301_12@ m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); @H_301_12@ Employeeobject=newEmployee(); @H_301_12@ object.setCode("CA"); @H_301_12@ object.setName("Cath"); @H_301_12@ object.setSalary(300); @H_301_12@ m.marshal(object,System.out); @H_301_12@ } @H_301_12@ }
@XmlRootElement
classEmployee{
privateStringcode;
privateStringname;
privateintsalary;
publicStringgetCode(){
returncode;
voidsetCode(Stringcode){
this.code=code;
publicStringgetName(){
returnname;
voidsetName(Stringname){
this.name=name;
intgetSalary(){
returnsalary;
voidsetSalary(intpopulation){
this.salary=population;
运行结果:
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <employee>
- code>CA</name>Cathsalary>300>