简单的Xml Framework松散映射不起作用

时间:2012-06-22 13:29:46

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

我正在尝试使用Simple Xml Framework解析部分xml文档,并设置了松散的映射标志但是我得到了一个异常。

XML:

<Body>
    <TopGoalScorersResponse>
        <TopGoalScorersResult>
            <tTopGoalScorer>
                <sName>Alan Dzagoev</sName>
                <iGoals>3</iGoals>
                <sCountry>Y</sCountry>
                <sFlag>http://footballpool.dataaccess.eu/images/flags/ru.gif</sFlag>
                <sFlagLarge>http://footballpool.dataaccess.eu/images/flags/ru.png</sFlagLarge>
            </tTopGoalScorer>
        </TopGoalScorersResult>
    </TopGoalScorersResponse>
</Body>

爪哇:

TopGoalScorer topGoalScorer = serializer.read(TopGoalScorer.class, xml);

@Root(name="tTopGoalScorer", strict=false)
public class TopGoalScorer {
    @Element(name="sName")
    private String name;

    @Element(name="iGoals")
    private int numGoals;

    @Element(name="sCountry")
    private String country;

    @Element(name="sFlag")
    private String flagImageUrl;
}

例外:

06-22 14:11:46.530: E/Soap(2057): Unable to satisfy @org.simpleframework.xml.Element(data=false, name=sCountry,     required=true, type=void) on field 'country' private java.lang.String uk.co.carr.david.TopGoalScorer.country for class uk.co.carr.david.TopGoalScorer at line 1
06-22 14:11:46.530: E/Soap(2057): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=sCountry, required=true, type=void) on field 'country' private java.lang.String uk.co.carr.david.TopGoalScorer.country for class uk.co.carr.david.TopGoalScorer at line 1

有什么想法吗?任何帮助将不胜感激。

由于 大卫

2 个答案:

答案 0 :(得分:3)

发生这种情况是因为你没有澄清元素的路径,只是尝试使用下一个结构,一切都会好的

    @Root(name="tTopGoalScorer", strict=false)
    public class TopGoalScorer {
        @Path("Body/TopGoalScorersResponse/TopGoalScorersResult/tTopGoalScorer/sName")
        @Element
        private String name;
    }

答案 1 :(得分:1)

更好的方法是使用Persister.read(MyClass.class,inputXml,false)。这告诉它忽略带注释的类中不匹配的任何内容。它比在@Root注释中使用strict = false更好,因为它适用于整个文档。