【问题标题】:Trouble deserializing XML using SimpleXML使用 SimpleXML 反序列化 XML 时遇到问题
【发布时间】:2013-05-27 06:49:55
【问题描述】:

我的类被注释为

@Root
public class Geonames {
    @Path("intersection/street1")
    @Element
    public String street1;

    @Path("intersection/highway1")
    @Element
    public String highway1;

    @Path("intersection/street2")
    @Element
    public String street2;

    @Path("intersection/highway2")
    @Element
    public String highway2;

    @Path("intersection/lat")
    @Element
    public String lat;

    @Path("intersection/lng")
    @Element
    public String lng;

    @Path("intersection/distance")
    @Element
    public String distance;
}

XML 是这样的:

<geonames>
    <intersection>
        <street1>11th Street</street1>
        <highway1>residential</highway1>
        <street2>Campbell Street</street2>
        <highway2>unclassified</highway2>
        <lat>37.8098123</lat>
        <lng>-122.2969874</lng>
        <distance>0.07</distance>
    </intersection>
</geonames>

但我得到了错误:

05-26 23:46:46.511: E//RequestProcessor.java:250(23618): Caused by:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy
@org.simpleframework.xml.Element(data=false, name=, required=true, type=void) on field
'street1' public java.lang.String com.mosier.securityapp.geonames.Geonames.street1 for
class com.mosier.securityapp.geonames.Geonames at line 4

这似乎表明 street1 没有价值,但它当然有。我的注释或类必须设置错误,但我对此太陌生,看不出发生了什么。有没有我忽略的简单修复方法?

【问题讨论】:

  • Point -- 在实现序列化/反序列化方案时,我总是先进行序列化以排除任何不正确的 XML,并且 -- 如果您在第一个元素上有错误,则很可能您有整个事情的一个根本问题。 -- 你需要一个地理名称/交叉点/.. 而不仅仅是你的@Path 中的一个交叉点/..
  • 我相信我也尝试过,得到了不同的错误。我能够通过将其作为嵌套对象而不是使用更复杂的@Paths 来解决此问题。关于序列化的好点。我会研究你的解决方案。

标签: android xml deserialization simple-framework


【解决方案1】:

试试这个

@Root
public class Geonames {
    @Path("intersection")
    @Element
    public String street1;

    @Path("intersection")
    @Element
    public String highway1;

    @Path("intersection")
    @Element
    public String street2;

    @Path("intersection")
    @Element
    public String highway2;

    @Path("intersection")
    @Element
    public String lat;

    @Path("intersection")
    @Element
    public String lng;

    @Path("intersection")
    @Element
    public String distance;
}

【讨论】:

  • 哦,真的。所以@Path 注解指定了元素的路径,减去元素本身。我想这就是我所缺少的。
猜你喜欢
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多