【问题标题】:Java : Simple XML not parsing the xml. Gives ExceptionJava:不解析 xml 的简单 XML。给出例外
【发布时间】:2012-12-16 18:29:18
【问题描述】:

我正在使用 Simple XML Serialization(simple-xml-2.6.6.jar) here 将我的 XML 响应从 web 服务转换为 POJO 类。 XML如下:

<Alerts>
        <Alert>
            <CCRDataObjectID>38</CCRDataObjectID>
            <DateTime>
                <Type>
                    <Text>Verified</Text>
                </Type>
                <ExactDateTime>2010-06-16T00:00:00Z</ExactDateTime>
            </DateTime>
            <Type>
                <Text>Allergy</Text>
            </Type>
            <Description>
                <Text>R-Tanna</Text>
            </Description>
            <Source>
                <Actor>
                    <ActorID>122</ActorID>
                </Actor>
            </Source>
            <Reaction>
                <Description>
                    <Text>rash</Text>
                </Description>
            </Reaction>
        </Alert>
        <Alert>
            <CCRDataObjectID>39</CCRDataObjectID>
            <DateTime>
                <Type>
                    <Text>Verified</Text>
                </Type>
                <ExactDateTime>2010-06-16T00:00:00Z</ExactDateTime>
            </DateTime>
            <Type>
                <Text>Allergy</Text>
            </Type>
            <Description>
                <Text>Zithromax</Text>
            </Description>
            <Source>
                <Actor>
                    <ActorID>122</ActorID>
                </Actor>
            </Source>
            <Reaction>
                <Description>
                    <Text>rash</Text>
                </Description>
            </Reaction>
        </Alert>
    </Alerts>

POJO 如下: 第一个 POJO 包含警报列表是 Alerts

@Root
public class Alerts {

    @ElementList
    private List<Alert> Alerts;

    public List<Alert> getAlerts() {
        return this.Alerts;
    }

    public void setAlerts(List<Alert> alerts) {
        this.Alerts = alerts;
    }

}

实际Alert 的第二个 POJO 如下:

@Root(strict=false)
public class Alert {

    @Element
    private int CCRDataObjectID;

    @Element
    private DateTime DateTime;  

    @Element
    private Type Type;  

    @Element 
    private Description Description;

    @Path("Source/Actor")
    @Element        
    private int ActorID;

    @Element
    private Reaction Reaction;

    @Root
    private static class Type {

        @Element
        private String Text;        

    }

    @Root
    private static class Description {

        @Element
        private String Text;
    }

    @Root
    private static class DateTime {

        @Element
        private Type Type;

        @Element
        private String ExactDateTime;

    }

    @Root
    private static class Reaction {

        @Element
        private Description Description;
    }

    public int getCCRDataObjectID() {
        return CCRDataObjectID;
    }

    public void setCCRDataObjectID(int cCRDataObjectID) {
        CCRDataObjectID = cCRDataObjectID;
    }   

    public String getVerification(){
        return this.DateTime.Type.Text;
    }

    public String getDateTime() {
        return this.DateTime.ExactDateTime;
    }

    public String getAllergyType() {
        return this.Type.Text;
    }
    /**
     * 
     * @return Name/Description of an Allergy
     */
    public String getDescription() {
        return this.Description.Text;
    }

    public int getActorID() {
        return ActorID;
    }

    public void setActorID(int actorID) {
        ActorID = actorID;
    }

    public String getReactionDescription() {
        return this.Reaction.Description.Text;
    }

    public String getDisplayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
        return sdf.format(this.DateTime.ExactDateTime);     
    }
}

解析时出现如下错误:

Element 'Alert' does not have a match in class com.mypck.pojo.Alerts at line 2

现在我无法更改 XML 响应,因为它正在其他地方使用。我可以用 POJO 做什么,以便解析我的 XML。

【问题讨论】:

    标签: java xml-parsing simple-framework


    【解决方案1】:

    好的。经过tutorial后得到答案。我只需要告诉该列表是内联在类 Alerts 中的。

    @Root
    public class Alerts {
    
        @ElementList(inline=true)
        private List<Alert> Alerts;
    
        public List<Alert> getAlerts() {
            return this.Alerts;
        }
    
        public void setAlerts(List<Alert> alerts) {
            this.Alerts = alerts;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      不应该这样吗:

      @ElementList
      private List<Allergy> Alerts;
      

      成为

      @ElementList
      private List<Alert> Alerts;
      

      ?

      【讨论】:

      • 实际上,如果我将来自 Web 服务的响应包装到一个标签中......比如说 .....,那么我的代码就可以正常工作。但正如我所说,我无法触摸 Web 服务代码......
      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2012-04-29
      相关资源
      最近更新 更多