问题
I am trying to parse part of an xml document using Simple Xml Framework with the loose mapping flag set but i get an exception.
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
Any ideas? Any help would be greatly appreciated.
Thanks David
回答1:
It happens because you didn't clarify path to the element, Just try to use next constructions and all will be OK
@Root(name="tTopGoalScorer", strict=false)
public class TopGoalScorer {
@Path("Body/TopGoalScorersResponse/TopGoalScorersResult/tTopGoalScorer/sName")
@Element
private String name;
}
回答2:
A better way to do this is Persister.read(MyClass.class, inputXml, false). This tells it to ignore anything not matched in the annotated class. Its better than using strict=false in the @Root annotation as it applies to the whole document.
来源:https://stackoverflow.com/questions/11157138/simple-xml-framework-loose-mapping-not-working