gpt4 book ai didi

java - 简单的 Xml 框架松散映射不起作用

转载 作者:行者123 更新时间:2023-11-29 03:45:24 25 4
gpt4 key购买 nike

我正在尝试使用设置了松散映射标志的简单 Xml 框架来解析 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>

Java:

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;
}

异常(exception):

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

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

谢谢大卫

最佳答案

发生这种情况是因为您没有明确元素的路径,只需尝试使用下一个构造就可以了

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

关于java - 简单的 Xml 框架松散映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11157138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com