1

私はアンドロイドが初めてなので、ばかげたことを尋ねている場合は申し訳ありません。私はアンドロイドで天気解析を行っています。SAX パーサーを使用しています。今、私はコンテンツ ハンドラに行き詰まっています。非常に多くのチュートリアルで、データを解析するためのアイデアを思いつきましたが、これらの例はすべて、タグに属性を含む XML ファイルに基づいていました。データを取得している場所から XML ファイルの場合、タグには属性がありませんが、子ノードがあります。ここで立ち往生しています。子ノードの値を取得する方法がわかりません。

   <data>
   <request>
   <type>City</type>
   <query>Peshawar, Pakistan</query>
   </request>
   <current_condition>
   <observation_time>01:04 PM</observation_time>
   <temp_C>20</temp_C>
   <temp_F>68</temp_F>
   <weatherCode>113</weatherCode>
   <weatherIconUrl>
   </weatherIconUrl>

「」と「」からデータを取得したいのですが、本当に見つかりません。助けてください。コンテンツ ハンドラ クラスのコードは次のとおりです。

public class HandlingXmlStuff extends DefaultHandler {

    XmlDataCollected info=new XmlDataCollected();

    public String getInformation() {        
        return info.dataToString();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub


        if(localName.equals("query")) {
            String city=....???;
            info.setCity(city);
        } else if(localName.equals("temp_f")) {
            String t=...???;
            int temp=Integer.parseInt(t);
            info.settemp(temp);
        }
    }
}
4

2 に答える 2

1

これは、指定された xml ファイルからデータを取得するためのソリューションです。

サンプルの weather.xml ファイル:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <request>
        <type>City</type>
        <query>Peshawar, Pakistan</query>
    </request>
    <current_condition>
        <observation_time>01:04 PM</observation_time>
        <temp_C>20</temp_C>
        <temp_F>68</temp_F>
        <weatherCode>113</weatherCode>
        <weatherIconUrl> a url </weatherIconUrl>
    </current_condition>
</data>

処理 XmlStuff.java :

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author visruth
 */
public class HandlingXmlStuff extends DefaultHandler {

    private boolean typeStatus;
    private boolean queryStatus;
    private boolean observation_timeStatus;
    private boolean temp_CStatus;
    private boolean temp_FStatus;
    private boolean weatherCodeStatus;
    private boolean weatherIconUrlStatus;
    private String type;
    private String query;
    private String observation_time;
    private String temp_C;
    private String temp_F;
    private String weatherCode;
    private String weatherIconUrl;

    public String getObservation_time() {
        return observation_time;
    }

    public String getQuery() {
        return query;
    }

    public String getTemp_C() {
        return temp_C;
    }

    public String getTemp_F() {
        return temp_F;
    }

    public String getType() {
        return type;
    }

    public String getWeatherCode() {
        return weatherCode;
    }

    public String getWeatherIconUrl() {
        return weatherIconUrl;
    }

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

        if (qName.equals("type")) {
            typeStatus = true;
        } else {
            typeStatus = false;
        }

        if (qName.equals("query")) {
            queryStatus = true;
        } else {
            queryStatus = false;
        }

        if (qName.equals("observation_time")) {
            observation_timeStatus = true;
        } else {
            observation_timeStatus = false;
        }
        if (qName.equals("temp_C")) {
            temp_CStatus = true;
        } else {
            temp_CStatus = false;
        }

        if (qName.equals("temp_F")) {
            temp_FStatus = true;
        } else {
            temp_FStatus = false;
        }

        if (qName.equals("weatherCode")) {
            weatherCodeStatus = true;
        } else {
            weatherCodeStatus = false;
        }
        if (qName.equals("weatherIconUrl")) {
            weatherIconUrlStatus = true;
        } else {
            weatherIconUrlStatus = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (typeStatus) {
            this.type = new String(ch, start, length).trim();
            typeStatus = false;
        }

        if (queryStatus) {
            this.query = new String(ch, start, length).trim();
            queryStatus = false;
        }
        if (observation_timeStatus) {
            this.observation_time = new String(ch, start, length).trim();
            observation_timeStatus = false;
        }
        if (temp_CStatus) {
            this.temp_C = new String(ch, start, length).trim();
            temp_CStatus = false;
        }
        if (temp_FStatus) {
            this.temp_F = new String(ch, start, length).trim();
            temp_FStatus = false;
        }
        if (weatherCodeStatus) {
            this.weatherCode = new String(ch, start, length).trim();
            weatherCodeStatus = false;
        }
        if (weatherIconUrlStatus) {
            this.weatherIconUrl = new String(ch, start, length).trim();
            weatherIconUrlStatus = false;
        }

    }

    public void parseDocument() {
        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();

            //parse the file and also register this class for call backs
            sp.parse(TestCase.class.getResource("wheather.xml").getPath(), this);
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}

次のコードを使用してデータを取得できます。

HandlingXmlStuff handlingXmlStuf = new HandlingXmlStuff();
handlingXmlStuf.parseDocument();
System.out.println("type:" + handlingXmlStuf.getType() + ":type");
System.out.println("observation_time:" + handlingXmlStuf.getObservation_time() + ":observation_time");
System.out.println("temp_C:" + handlingXmlStuf.getTemp_C() + ":temp_C");
System.out.println("temp_F:" + handlingXmlStuf.getTemp_F() + ":temp_F");
System.out.println("weatherCode:" + handlingXmlStuf.getWeatherCode() + ":weatherCode");
System.out.println("weatherIconUrl:" + handlingXmlStuf.getWeatherIconUrl() + ":weatherIconUrl");
于 2013-01-24T19:01:22.727 に答える