在android中的Sax解析器问题

时间:2012-03-08 11:28:50

标签: java android

我正在尝试使用SAX解析器解析xml。代码在PC上正常工作,但在android上,元素不会被添加到列表中。 在代码中我试图在标签中添加数据sunrise&日落到列表数组

公开场合      void endElement(..){}

System.out.println(“list of list”+ timeLst.size()); //总是在android中显示0

以下是代码..

TimeServiceParser tsp = new TimeServiceParser();
tsp.parseDocument(new URL("http://www.earthtools.org/sun/47.566667/-52.716667/14/3/99/1"));
tsp.printData();


public class TimeService extends DefaultHandler {

   public void parseDocument(URL sourceUrl) {
        SAXParserFactory spf =  SAXParserFactory.newInstance();

        try{
            SAXParser sp = spf.newSAXParser();
            InputStream is = sourceUrl.openStream();    
            sp.parse(is, this);
        }catch(SAXException se) {               
                ...         
        }

  }


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

        tempVal = "";
        if(qName.equalsIgnoreCase("sunrise")) {
                tempTimeData = new TimeData();                      
        }
    }


    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(qName.equalsIgnoreCase("sunrise")) {
                tempTimeData.setSunriseTime(tempVal);
                timeLst.add(tempTimeData);

        }else if(qName.equalsIgnoreCase("sunset")) {
                if(tempTimeData!=null) {
                       TimeData t = (TimeData)(timeLst.get(0));
                       t.setSunsetTime(tempVal);
                }
        }

        System.out.println("size of list " + timeLst.size());   //Always shows 0 in android 
    }


    public void characters(char[] ch, int start, int length) throws SAXException {          
        tempVal = new String(ch , start , length);  

    }

    public void printData() {

        Iterator<TimeData> it = timeLst.listIterator();

        while(it.hasNext()) {
                TimeData td = (TimeData)(it.next());
                System.out.println(td.getSunriseTime());    
                System.out.println(td.getSunsetTime());
        }
    }

}

0 个答案:

没有答案