用JAXB从对象生成XML

前端之家收集整理的这篇文章主要介绍了用JAXB从对象生成XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Java代码
  1. importjava.io.FileOutputStream;
  2. @H_301_16@importjavax.xml.bind.JAXBContext; @H_301_16@importjavax.xml.bind.Marshaller; @H_301_16@importjavax.xml.bind.annotation.XmlRootElement; @H_301_16@publicclassJavaToXMLDemo{ @H_301_16@staticvoidmain(String[]args)throwsException{
  3. JAXBContextcontext=JAXBContext.newInstance(Employee.class);
  4. Marshallerm=context.createMarshaller();
  5. m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  6. Employeeobject=newEmployee();
  7. object.setCode("CA");
  8. object.setName("Cath");
  9. object.setSalary(300);
  10. m.marshal(object,System.out);
  11. }
  12. }

@XmlRootElement @H_301_16@classEmployee{ @H_301_16@privateStringcode; @H_301_16@privateStringname; @H_301_16@privateintsalary; @H_301_16@publicStringgetCode(){ @H_301_16@returncode; @H_301_16@voidsetCode(Stringcode){ @H_301_16@this.code=code; @H_301_16@publicStringgetName(){ @H_301_16@returnname; @H_301_16@voidsetName(Stringname){ @H_301_16@this.name=name; @H_301_16@intgetSalary(){ @H_301_16@returnsalary; @H_301_16@voidsetSalary(intpopulation){ @H_301_16@this.salary=population;
  • 运行结果:

    Xml代码
      <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
    1. <employee>
    2. code>CA</name>Cathsalary>300>
    原文链接:https://www.f2er.com/xml/299966.html
  • 猜你在找的XML相关文章