對了對了,最近還接觸了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 定義映射這個類中的何種類型需要映射到XML。可接收四個參數,分别是:
XmlAccessType.FIELD:映射這個類中的所有字段到XML
XmlAccessType.PROPERTY:映射這個類中的屬性(get/set方法)到XML
XmlAccessType.PUBLIC_MEMBER:将這個類中的所有public的field或property同時映射到XML(預設)
XmlAccessType.NONE:不映射
@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");
//将javabean轉換成xml(jaxb)
JAXBContext context = JAXBContext.newInstance(Body.class);
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>
暫時我就接觸這麼多,還會後續補充的。
加油,我們是最棒的仙女。
加一段将xml轉換成javabean的代碼
public static void unmarshallerByJaxb(String path, Object object) {
try {
JAXBContext jc = JAXBContext.newInstance(object.getClass());
Unmarshaller unmarshaller = jc.createUnmarshaller();
File file = new File(path);
if (!file.exists()) {
throw new FileNotFoundException("Can not load xml file!");
}
unmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}