0

saxparserを使用してyahooweather apiを解析しています

<yweather:forecast day="Sun" date="30 Jun 2013" low="22" high="34" text="Sunny" code="32"/>
<yweather:forecast day="Mon" date="1 Jul 2013" low="22" high="33" text="Sunny" code="32"/>
<yweather:forecast day="Tue" date="2 Jul 2013" low="22" high="33" text="Partly Cloudy" code="30"/>
<yweather:forecast day="Wed" date="3 Jul 2013" low="23" high="36" text="Sunny" code="32"/>

このコードを使用して:

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if (qName.equalsIgnoreCase("yweather:forecast")){

        day1.add(attributes.getValue("day")); 
        day1.add(attributes.getValue("date"));
        day1.add(attributes.getValue("low"));
        day1.add(attributes.getValue("high"));
        day1.add(attributes.getValue("text"));
        day1.add(attributes.getValue("code"));      

        info.setweather(day1.get(0),day1.get(1),day1.get(2),day1.get(3),day1.get(4),day1.get(5));
    }
}

初日を迎えられるようになりました。しかし、私は2日目だけを取得し、他の4日も取得して別の配列に入れたいと思っています。

4

1 に答える 1

0

XML ノードの順序だけで、2 日目が何であるかを把握できると仮定します。

yweather:forecast要素の開始を見つけた頻度を数えるだけです。

private int currentDay;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if (qName.equalsIgnoreCase("yweather:forecast")){
        if (currentDay++ != 1) return;
        // rest of your code

また、ドキュメントの開始または終了ごとにカウンターをリセットまたは初期化します。

于 2013-06-30T00:38:48.790 に答える