簡體   English   中英

SimpleXMLConverter解析xml節點

[英]SimpleXMLConverter parse xml nodes

我被卡住了。 如何使用同名子節點解析Node? 在這個例子中,我需要具有rate屬性的節點。

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

我的響應包裝類:

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

你是正確的方式。 由於這里有很多比率,因此可以通過更多上下文更好地命名類,並通過Annotation設置xml名稱。

我已經分離了XMLResponseRates類,但這沒有任何區別。

XMLRespone類映射<xml>...</xml>部分:

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

    // ...
}

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

        // ...
    }

}

反序列化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