欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jaxb将javabean转化成xml

程序员文章站 2022-03-31 16:59:57
...

对了对了,最近还接触了jaxb,这个是啥呢,就是xml与JavaBean的相互转换。
最近才知道,就算参数是xml,那也还是String类型。xml只是个格式,只是xml格式的String而已啦。
先来说一下我到的jaxb注解。
@XmlRootElement.将java类或枚举映射成xml元素根节点,是唯一一个必须注解。name属性指定根节点名称,不写就默认类名小写。
@XmlElement.将java类的一个属性映射为xml节点元素,name属性可自定义元素名。
@XmlAttribute.将java的一个属性映射为xml节点的属性,name属性可自定义属性名。
@XmlType. 将java类或枚举映射到xml模式类型。常与@[email protected]共用,proOrder属性定义字段生成的xml节点顺序。
@XmlAccessorType.控制字段或属性序列化。@XmlAccessorType(XmlAccessType.PROPERTY).只有属性才能被转换成 xml 中的标签。
@XmlElementWrapper.对于数组或集合,生成一个包装该数组或集合的xml元素,该注解只能用在集合上。
概念对我这种小白来说,简直就是无字天书,所以说,上代码。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "BaseInfo")
public class BaseInfo {
    @XmlElement(name = "CardID")
    private String cardID;

    @XmlElement(name = "PatientName")
    private String patientName;

    @XmlElement(name = "BirthDate")
    private String birthDate;

    @XmlElement(name = "GenderCode")
    private String genderCode;

    @XmlElement(name = "NationCode")
    private String nationCode;

    @XmlElement(name = "EmployerOrgName")
    private String employerOrgName;

    @XmlElement(name = "IDR_OccupationCode")
    private String idr_OccupationCode;

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }
    public String getPatientName() {
        return patientName;
    }
    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }
    public String getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
    public String getGenderCode() {
        return genderCode;
    }
    public void setGenderCode(String genderCode) {
        this.genderCode = genderCode;
    }
    public String getNationCode() {
        return nationCode;
    }
    public void setNationCode(String nationCode) {
        this.nationCode = nationCode;
    }
    public String getEmployerOrgName() {
        return employerOrgName;
    }
    public void setEmployerOrgName(String employerOrgName) {
        this.employerOrgName = employerOrgName;
    }
    public String getIdr_OccupationCode() {
        return idr_OccupationCode;
    }
    public void setIdr_OccupationCode(String idr_OccupationCode) {
        this.idr_OccupationCode = idr_OccupationCode;
    }
    public String getOtherOccupationName() {
        return otherOccupationName;
    }
    public void setOtherOccupationName(String otherOccupationName) {
        this.otherOccupationName = otherOccupationName;
    }
    public String getGuardianName() {
        return guardianName;
    }
    public void setGuardianName(String guardianName) {
        this.guardianName = guardianName;
    }

    public String getTeleCom() {
        return teleCom;
    }

    public void setTeleCom(String teleCom) {
        this.teleCom = teleCom;
    }

    public String getOrgCountyCode() {
        return orgCountyCode;
    }

    public void setOrgCountyCode(String orgCountyCode) {
        this.orgCountyCode = orgCountyCode;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }

    public String getLivingAddressAttributionCode() {
        return livingAddressAttributionCode;
    }

    public void setLivingAddressAttributionCode(String livingAddressAttributionCode) {
        this.livingAddressAttributionCode = livingAddressAttributionCode;
    }

    public String getLivingAddressCode() {
        return livingAddressCode;
    }

    public void setLivingAddressCode(String livingAddressCode) {
        this.livingAddressCode = livingAddressCode;
    }

    public String getLivingAddressDetails() {
        return livingAddressDetails;
    }

    public void setLivingAddressDetails(String livingAddressDetails) {
        this.livingAddressDetails = livingAddressDetails;
    }

    public String getIdcardCode() {
        return idcardCode;
    }

    public void setIdcardCode(String idcardCode) {
        this.idcardCode = idcardCode;
    }
}
public class BaseInfoTest extends BaseTest {
    @Autowired
    Jaxb2Marshaller marshaller;
    @Test
    public void getCardID() throws JAXBException{
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setCardID("1");
        baseInfo.setPatientName("65");
        baseInfo.setBirthDate("99");
        baseInfo.setGenderCode("555");
        baseInfo.setNationCode("99");
        baseInfo.setEmployerOrgName("7878");
        baseInfo.setEmployerOrgName("987554");
        baseInfo.setIdr_OccupationCode("sdfdsf");
        JAXBContext context = JAXBContext.newInstance(Body.class);
        //将javabean转换成xml
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        StringWriter writer = new StringWriter();
        marshaller.marshal(baseInfo, writer);
        System.out.println(writer.toString());
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BaseInfo>
<CardID>1</CardID>
<PatientName>65</PatientName>
<BirthDate>99</BirthDate>
<GenderCode>555</GenderCode>
<NationCode>99</NationCode>
<EmployerOrgName>987554</EmployerOrgName>
<IDR_OccupationCode>sdfdsf</IDR_OccupationCode>
</BaseInfo>

暂时我就接触这么多,还会后续补充的。
加油,我们是最棒的仙女。