1

我正在尝试使用设置了松散映射标志的 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

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

谢谢大卫

4

2 回答 2

3

发生这种情况是因为您没有明确元素的路径,只需尝试使用下一个构造,一切都会好的

    @Root(name="tTopGoalScorer", strict=false)
    public class TopGoalScorer {
        @Path("Body/TopGoalScorersResponse/TopGoalScorersResult/tTopGoalScorer/sName")
        @Element
        private String name;
    }
于 2012-06-22T14:14:59.063 回答
1

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

于 2012-11-10T14:03:17.210 回答