Java:简单的XML不解析xml。给出例外

时间:2013-01-02 11:25:37

标签: java xml-parsing simple-framework

我正在使用Simple XML Serialization(simple-xml-2.6.6.jar)here将我的XML响应从webservice转换为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);     
    }
}

On Parsing我得到如下错误:

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

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

2 个答案:

答案 0 :(得分:3)

行。经过tutorial后得到答案。我只需要告诉列表是在类警报中内联。

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

}

答案 1 :(得分:1)

不应该这样:

@ElementList
private List<Allergy> Alerts;

@ElementList
private List<Alert> Alerts;