Java:不解析 xml 的简单 XML.给出异常 [英] Java : Simple XML not parsing the xml. Gives Exception

查看:20
本文介绍了Java:不解析 xml 的简单 XML.给出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Simple XML Serialization(simple-xml-2.6.6.jar) here 来转换我的从 Web 服务到 POJO 类的 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 as follow:

<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

POJOs are as follow : 1st POJO containig list of Alert is 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如下:

2nd POJO of actual Alert is as follow :

@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 I get error as follow :

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

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

Now I can't change the XML response as It is being used at other places. What can I do with POJO so that I can parse my XML.

推荐答案

好的.通过教程后得到了答案.我只需要说明该列表是内嵌在 Alerts 类中的.

OK. Got the answer after going through tutorial. I just need to tell that the list is inline in class 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;
    }

}

这篇关于Java:不解析 xml 的简单 XML.给出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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