0

我收到一个 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 行的类 parsing.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;
}
4

1 回答 1

1

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

于 2013-02-18T10:16:49.093 回答