gpt4 book ai didi

Java : Simple XML Serialization(simple-xml-2. 6.6.jar) 包含 & 的 XML 文件出错

转载 作者:行者123 更新时间:2023-11-30 09:31:18 27 4
gpt4 key购买 nike

我正在使用简单 XML 序列化 (simple-xml-2.6.6.jar) here将我的 XML 响应从 webservice 转换为 POJO 类。 XML 是:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<SOAP-ENV:Body>
<return>
<appointments>
<appointment>
<encounterId>211707</encounterId>
<date>2012-10-16</date>
<startTime>00:00:00</startTime>
<ufname>Sam</ufname>
<ulname>Willis</ulname>
<reason>Chest Congestion</reason>
<FacilityId>2837</FacilityId>
<Notes/>
</appointment>
</appointments>
</return>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

约会包含约会列表。 POJO 如下:

@Root
public class Return {

@ElementList
private List<Appointment> appointments;

@ElementList
private List<Appointment> historicalAppointments;

public List<Appointment> getAppointments() {
return appointments;
}

public void setAppointments(List<Appointment> appointments) {
this.appointments = appointments;
}

public List<Appointment> getHistoricalAppointments() {
return historicalAppointments;
}

public void setHistoricalAppointments(List<Appointment> historicalAppointments) {
this.historicalAppointments = historicalAppointments;
}
}

约会是:

@Root
public class Appointment {

@Element(required=false)
int encounterId;

@Element
Date date;

@Element
String startTime;

@Element(required=false)
String endTime;

@Element
String ufname;

@Element
String ulname;

@Element(required=false)
int FacilityId;

@Element(required=false)
String reason;

@Element(required=false)
String Notes;

@Element(required=false)
String Status;

@Element(required=false)
int EncLock;

public int getEncounterId() {
return encounterId;
}

public void setEncounterId(int encounterId) {
this.encounterId = encounterId;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getStartTime() {
return startTime;
}

public void setStartTime(String startTime) {
this.startTime = startTime;
}

public String getEndTime() {
return endTime;
}

public void setEndTime(String endTime) {
this.endTime = endTime;
}

public String getUfname() {
return ufname;
}

public void setUfname(String ufname) {
this.ufname = ufname;
}

public String getUlname() {
return ulname;
}

public void setUlname(String ulname) {
this.ulname = ulname;
}

public int getFacilityId() {
return FacilityId;
}

public void setFacilityId(int facilityId) {
FacilityId = facilityId;
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public String getNotes() {
return Notes;
}

public void setNotes(String notes) {
Notes = notes;
}

public String getStatus() {
return Status;
}

public void setStatus(String status) {
Status = status;
}

public int getEncLock() {
return EncLock;
}

public void setEncLock(int encLock) {
EncLock = encLock;
}


}

现在如果我删除 <SOAP-ENV:Envelope> & <SOAP-ENV:Body>从我的 XML 中它工作正常。但是当使用这些标签解析 XML 时,我收到错误消息:

Exception in thread "main" org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class pojo.Return at line 3

最佳答案

Now If I remove <SOAP-ENV:Envelope> & <SOAP-ENV:Body> from my XML it works fine. But when parsing XML with these tag I get the error.

这就是你的问题。简单 XML 尝试解析您提供给它的整个文档。如果你说:

serializer.read(Appointment.class, yourXML)

那个 XML 的根是 <SOAP-ENV:Envelope>其次是 <SOAP-ENV:Body>那么它是行不通的。您需要制作一个或多个类来封装 <SOAP-ENV:*> XML 中的节点。否则 Simple XML 会感到困惑,并告诉您它没有看到它期望看到的内容。这就是当您删除这些元素时它起作用的原因:因为 XML 现在看起来完全像它预期的那样。

关于Java : Simple XML Serialization(simple-xml-2. 6.6.jar) 包含 <SOAP-ENV:Body> & <SOAP-ENV:Envelope> 的 XML 文件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947320/

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