简体   繁体   English

SimpleXMLConverter解析xml节点

[英]SimpleXMLConverter parse xml nodes

I'm stuck. 我被卡住了。 How to parse Node with same name child node? 如何使用同名子节点解析Node? In this example i need nodes with rate attribute. 在这个例子中,我需要具有rate属性的节点。

<xml>
<Rates>
  <Rates winrate_cap="9999">
    <Rates rate="323"/>
    <Rates rate="343"/>
    <Rates rate="2338"/>
    <Rates rate="233"/>
  </Rates>
</Rates>
</xml>

My response wrapper class: 我的响应包装类:

@Root(name = "xml", strict = false)
public class XMLResponse {

    @ElementList(entry = "Rates")
    public List<Rates> response;

    public static class Rates {

        @Attribute(name = "winrate_cap", required = false)
        public String winrate_cup;

        @ElementList(required = false, entry = "Rates")
        public List<Rates> rates;
    }

    public static class Rates {
        @Attribute(name = "rate", required = false)
        public String rate;
    }
}

You are on the right way. 你是正确的方式。 As you have many Rates here, better name the class more with more context and set the xml name via Annotation. 由于这里有很多比率,因此可以通过更多上下文更好地命名类,并通过Annotation设置xml名称。

I've separated the XMLResponse and Rates classes, but this doesn't make any difference. 我已经分离了XMLResponseRates类,但这没有任何区别。

The class XMLRespone maps the <xml>...</xml> part: XMLRespone类映射<xml>...</xml>部分:

@Root(name = "xml")
public class XMLRespone
{
    @Element(name = "Rates")
    private Rates rates;

    // ...
}

All types for <Rates...>...</Rates> is done by Rates class (and it's inner classes, which map the structure: <Rates...>...</Rates>所有类型都由Rates类完成(它的内部类映射结构:

@Root(name = "Rates")
public class Rates
{
    @Element(name = "Rates")
    private RateList rates;

    // ...


    public static class RateList
    {
        @ElementList(entry = "Rates", inline = true)
        private ArrayList<RateValue> values;
        @Attribute(name = "winrate_cap", required = true)
        private int winrateCap;

        // ...
    }


    public static class RateValue
    {
        @Attribute(name = "rate", required = true)
        private int rate;

        // ...
    }

}

Deserializing your XML gives this output (using generated toString() ): 反序列化XML会产生此输出(使用生成的toString() ):

XMLRespone{rates=Rates{rates=RateList{values=[RateValue{rate=323}, RateValue{rate=343}, RateValue{rate=2338}, RateValue{rate=233}], winrateCap=9999}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM