简体   繁体   English

在JAva中使用简单XML框架解析XML

[英]Parsing XML using Simple XML Framework in JAva

I am trying to parse some XML using the Simple XML framework, however I'm having trouble with getting it work. 我正在尝试使用简单XML框架解析一些XML,但是我无法使其正常工作。 This is the model I used: 这是我使用的模型:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "rootelement", strict = false)
public class XMLModel {
    @Element(name = "tag")
    private TagModel tag;

    @Element(name = "id", required = false)
    private String id;

    @Element(name = "url", required = false)
    private String url;

    public TagModel getTag() {
        return tag;
    }

    public void setTag(TagModel tag) {
        this.tag = tag;
    }

    public String getId() {
        return id;
    }

    public void setId(String videoId) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

And the TagModel file is: TagModel文件是:

import org.simpleframework.xml.Attribute;

public class TagModel implements Parcelable {
    @Attribute(name = "data", required = false)
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

Here's the XML I'm trying to parse. 这是我要解析的XML。

<?xml version="1.0" encoding="UTF-8"?>
<rootelement version="1.0">
    <tag data="none"/>
    <id><![CDATA[95757410]]></id>
    <url><![CDATA[http://google.com]]></url>
</rootelement>

I get the following error with the current code: 我在当前代码中收到以下错误:

retrofit.RetrofitError: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class

If anyone would know how I could get the various data (including the TagModel) I'd really appreciate it! 如果有人知道如何获取各种数据(包括TagModel),我将不胜感激!

Thanks for the help, Daniel 感谢您的帮助,丹尼尔

I realised the error was due to not having a constructor in the models. 我意识到该错误是由于模型中没有构造函数所致。

For example, in TagModel I needed to add the following: 例如,在TagModel中,我需要添加以下内容:

public TagModel(@Attribute(name = "data") String data) {
    this.data = data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM