gpt4 book ai didi

Java:如何将从数据库返回的字符串转换为 XML

转载 作者:行者123 更新时间:2023-12-01 10:55:51 25 4
gpt4 key购买 nike

我正在尝试编写一个方法来访问数据库,然后以 XML 格式返回字符串/对象,这是我迄今为止所拥有的方法。我看过 POJO,但我不相信这就是我要找的东西。我需要循环并检索每个 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>

最佳答案

查看此问题:Binding XML using POJO and JAXB annotations

您可以创建自定义 POJO 并使用 JAXB 对其进行注释以获得所需的输出

关于Java:如何将从数据库返回的字符串转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com