Java的:简单的XML序列化(简单的xml-2.6.6.jar)给出了错误包含XML文件< SOAP-ENV:身体与GT; &安培; < SOAP-ENV:信封> [英] Java : Simple XML Serialization(simple-xml-2.6.6.jar) gives error with XML file containing <SOAP-ENV:Body> & <SOAP-ENV:Envelope>

查看:218
本文介绍了Java的:简单的XML序列化(简单的xml-2.6.6.jar)给出了错误包含XML文件< SOAP-ENV:身体与GT; &安培; < SOAP-ENV:信封>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单的XML序列化(简单的xml-2.6.6.jar)这里转换我XML响应从Web服务到POJO类。
 XML是:

 < SOAP-ENV:信封的xmlns:SOAP-ENV =htt​​p://schemas.xmlsoap.org/soap/envelope/的xmlns:XSD =HTTP:// www.w3.org/1999/XMLSchema的xmlns:XSI =http://www.w3.org/1999/XMLSchema-instance>
      < SOAP-ENV:身体与GT;
         <返回>
           <任命及GT;
             <任命>
               < encounterId> 211707< / encounterId>
               <日期和GT; 2012年10月16日< /日期和GT;
               &所述; startTime时> 00:00:00&下; / startTime时>
               < ufname>萨姆和LT; / ufname>
               < ulname>威利斯< / ulname>
               <&原因GT;胸闷< /&原因GT;
               < FacilityId> 2837< / FacilityId>
               <注意/>
             < /预约>
          < /预约>
        < /返回>
      < / SOAP-ENV:身体与GT;
 < / SOAP-ENV:信封>

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

  @root
公共类返回{    @ElementList
    私人列表<任命>任命;    @ElementList
    私人列表<任命> historicalAppointments;    公开名单<任命> getAppointments(){
        返回任命;
    }    公共无效setAppointments(列表<任命>任命){
        this.appointments =约会;
    }    公开名单<任命> getHistoricalAppointments(){
        返回historicalAppointments;
    }    公共无效setHistoricalAppointments(列表<任命> historicalAppointments){
        this.historicalAppointments = historicalAppointments;
    }
}

和任命是:

  @root
公共类预约{    @Element(必填= FALSE)
    INT encounterId;    @元件
    日期日期;    @元件
    字符串的startTime;    @Element(必填= FALSE)
    字符串结束时间;    @元件
    串ufname;    @元件
    串ulname;    @Element(必填= FALSE)
    INT FacilityId;    @Element(必填= FALSE)
    字符串的原因;    @Element(必填= FALSE)
    字符串说明;    @Element(必填= FALSE)
    字符串状态;    @Element(必填= FALSE)
    INT EncLock;    公众诠释getEncounterId(){
        返回encounterId;
    }    公共无效setEncounterId(INT encounterId){
        this.encounterId = encounterId;
    }    公开日期GETDATE(){
        归期;
    }    公共无效的setDate(日期为准){
        this.date =日期;
    }    公共字符串getStartTime(){
        返回的startTime;
    }    公共无效setStartTime(字符串的startTime){
        this.startTime =的startTime;
    }    公共字符串getEndTime(){
        返回结束时间;
    }    公共无效setEndTime(字符串结束时间){
        this.endTime =结束时间;
    }    公共字符串getUfname(){
        返回ufname;
    }    公共无效setUfname(字符串ufname){
        this.ufname = ufname;
    }    公共字符串getUlname(){
        返回ulname;
    }    公共无效setUlname(字符串ulname){
        this.ulname = ulname;
    }    公众诠释getFacilityId(){
        返回FacilityId;
    }    公共无效setFacilityId(INT facilityId){
        FacilityId = facilityId;
    }    公共字符串getReason(){
        回归理性;
    }    公共无效setReason(字符串的原因){
        this.reason =原因;
    }    公共字符串getNotes(){
        返回注意;
    }    公共无效setNotes(字符串注){
        注=说明;
    }    公共字符串的getStatus(){
        返回状态;
    }    公共无效setStatus(字符串状态){
        状态=地位;
    }    公众诠释getEncLock(){
        返回EncLock;
    }    公共无效setEncLock(INT encLock){
        EncLock = encLock;
    }
}

现在如果我删除< SOAP-ENV:信封> &安培; < SOAP-ENV:身体与GT; 从我的XML,它工作正常。但这些标签解析XML时,我得到的错误说:

 异常线程mainorg.simpleframework.xml.core.ElementException:元素'身体'没有在第3行类pojo.Return比赛


解决方案

  

现在如果我删除< SOAP-ENV:信封> &安培; < SOAP-ENV:身体与GT; 从我的XML,它工作正常。但这些标签解析XML时,我得到的错误。


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

  serializer.read(Appointment.class,yourXML)

和XML的根是< SOAP-ENV:信封> 后跟一个< SOAP-ENV:身体与GT; 则是行不通的。你需要做一个或多个类来封装< SOAP-ENV:*> 你XML节点。否则,简单的XML将会感到困惑,并告诉你,这是没有看到什么希望看到的。这就是为什么它的工作原理,当你删除这些元素:因为XML现在看起来完全一样,它预计它是

I am using Simple XML Serialization(simple-xml-2.6.6.jar) here to convert my XML response from webservice to POJO class. XML is :

 <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>

Appointments contains list of appointments. POJOs are as follow :

@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;
    }   
}

And Appointment is :

@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;
    }


}

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 saying :

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.

And that is your problem right there. Simple XML attempts to parse the entire document that you give it. If you say:

serializer.read(Appointment.class, yourXML)

and the root of that XML is <SOAP-ENV:Envelope> followed by a <SOAP-ENV:Body> then it is not going to work. You need to make one or more classes to encapsulate the <SOAP-ENV:*> nodes in you XML. Otherwise Simple XML is going to get confused and tell you that it is not seeing what it expected to see. That is why it works when you remove those elements: because the XML now looks exactly like it expected it to be.

这篇关于Java的:简单的XML序列化(简单的xml-2.6.6.jar)给出了错误包含XML文件&LT; SOAP-ENV:身体与GT; &安培; &LT; SOAP-ENV:信封&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆