【问题标题】:Simplexml valuerequired exceptionSimplexml 值必需的异常
【发布时间】:2013-02-15 17:34:51
【问题描述】:

我得到一个我无法理解的 valuerequired 异常错误。如果我将 xmlns 设置为 required = false 一切正常,但 xmlns 值为空,尽管所有其他值都按预期设置。

我正在阅读的xml:

<test xmlns="somestring"           
id="123456"   
timestamp="2013-05">         
<value1>12</value1>     
<value2>3 </value2>    
</test>

这是错误信息:

02-15 18:05:55.988: W/System.err(31450): org.simpleframework.xml.core.ValueRequiredException: 无法满足 @org.simpleframework.xml.Attribute(empty=, name=xmlns, required=true) 字段 'xmlns' 私有 java.lang.String parsing.Test.xmlns 用于第 1 行的类解析.Test

代码:

 @Root(name = "test") 
public class Test {


    @Element(name = "value1")
    private String value1;
    @Element(name ="value2")
    private String value2;

    @Attribute(name = "xmlns")
    private String xmlns;
    @Attribute(name = "id")
    private String id;
    @Attribute(name = "timestamp")
    private String timestamp;



    public Test () {
    }

    public Test (String value1, String value2, String xmlns, String id, String timestamp) {
        this.value1 = value1;
        this.value2 = value2;
        this.xmlns = xmlns;
        this.id = id;
        this.timestamp = timestamp;
    }

    public String getValue1() {
        return value1;
    }

    public String getValue2 () {
        return value2;
    }
    public String getXMLns () {
        return xmlns;
    }
    public String getId () {
        return id;
    }
    public String getTimeStamp () {
        return timestamp;
    }}

序列化方法:

public Test DeSerialize (String xml) {
    //System.out.println("xml:  \n " + xml);
    try {
        test = serializer.read(Test.class, xml);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return test;
}

【问题讨论】:

    标签: java android xml xml-parsing simple-framework


    【解决方案1】:

    属性 xmlns 是为命名空间保留的,这就是你得到错误的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多