0
我想寫一個訪問數據庫,然後返回XML格式的字符串/對象的方法這裏是我有這樣的方法遠。我看過POJO,我不相信我在找什麼。我需要遍歷並檢索每個xml子節點。然後以正確的格式顯示它。Java:我如何將從數據庫返回的字符串轉換爲XML
15 public Object getAppointment(String patientid){
16 DB db = new DB();
17 List<Object> objs = db.getData("Appointment", "patientid='"+patientid+"'");
18 Patient patient = null;
19 Phlebotomist phleb = null;
20 PSC psc = null;
21 for (Object obj : objs){
22 patient = ((Appointment)obj).getPatientid();
23 phleb = ((Appointment)obj).getPhlebid();
24 psc = ((Appointment)obj).getPscid();
25 return obj;
26 }
27 return "";
28 }
我需要在XML格式的obj變量,但我不能弄明白。這是它現在返回的內容。
Appointment[id=710, Date: 2004-02-01, Time: 13:00:00
Phlebotomist: Phlebotomist[id=110, Name: Elizabeth Corday]
Patient: Patient[id=220, Name: Alice Wonderland, Address: 201 Mt. Hope Avenue, Insurance? Y, DOB: 1985-10-12
Physician: Physician[id=20, Name: Dr. Fine]]
PSC: PSC[id=510, name= Outer Banks]]
Test: AppointmentLabTest[appointmentLabTestPK=AppointmentLabTestPK[apptid=710, labtestid=86900, dxcode=292.9]
Lab Test: LabTest[id=86900, Name: Blood Group & Rh Type, whole BloodCost: 15.0]
Diagnosis: Diagnosis[code=292.9, Name: Caffeine – Related Disorder NOS]]
從數據庫返回時,我需要它看起來像這樣。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<AppointmentList>
<appointment date="2016-12-30" id="791" time="10:00:00">
<patient id="220">
<name>Alice Wonderland</name>
<address>201 Mt. Hope Avenue</address>
<insurance>Y</insurance>
<dob>1985-10-12</dob>
</patient>
<phlebotomist id="110">
<name>Elizabeth Corday</name>
</phlebotomist>
<psc id="520">
<name>Down Town</name>
</psc>
<allLabTests>
<appointmentLabTest apptointmentId="791" dxcode="307.3" labTestId="86609"/>
<appointmentLabTest apptointmentId="791" dxcode="292.9" labTestId="86900"/>
</allLabTests>
</appointment>
</AppointmentList>
您能解釋一下這個嗎?我不太明白如何做到這一點。 –