2012-05-21 3 views
0

Android 용 SAX 파서를 만들려고하고 있으며 외부 XML 파일에서 하나의 태그 만 읽음으로써 시작했습니다. '불행히도, - 중지되었습니다'오류가 발생합니다. 로그 파일을 보았는데 null 참조 오류가 발생합니다. 내 생각 엔 XMLAdapter 클래스가 제대로 작동하지 않고 문제를 파악할 수 없었습니다.Android SAX 파서 오류

package com.vint.michiganbus; 

import java.net.URL; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.InputSource; 
import org.xml.sax.XMLReader; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.view.Window; 
import android.util.Log; 


public class listView extends Activity { 
    XMLGettersSetters data; 
    private static final String TAG = "listView"; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     View layout = findViewById(R.id.layout); 

     TextView title[]; 

     Log.i(TAG, "data is hello"); 
     try { 
      /** 
      * Create a new instance of the SAX parser 
      **/ 
      SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
      SAXParser saxP = saxPF.newSAXParser(); 
      XMLReader xmlR = saxP.getXMLReader(); 

      URL url = new URL("http://mbus.pts.umich.edu/shared/public_feed.xml"); // URL of the XML 

      /** 
      * Create the Handler to handle each of the XML tags. 
      **/ 
      XMLHandler myXMLHandler = new XMLHandler(); 
      xmlR.setContentHandler(myXMLHandler); 
      xmlR.parse(new InputSource(url.openStream())); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 

     data = XMLHandler.data; 

     /** 
     * Makes the TextView length the size of the TextView arrays by getting the size of the 
     **/ 
     title = new TextView[data.get().size()];   

     /** 
     * Run a for loop to set All the TextViews with text until 
     * the size of the array is reached. 
     * 
     **/ 
     for (int i = 0; i < data.get().size(); i++) { 

      title[i] = new TextView(this); 
      title[i].setText("Title = "+data.get().get(i)); 

      ((ViewGroup) layout).addView(title[i]); 
      } 

     setContentView(layout); 

     //setContentView(R.layout.listview); 
    } 
} 

가 여기 내 XML 처리기이다 :

여기 내 주요 활동이다

package com.vint.michiganbus; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 
import android.util.Log; 

public class XMLHandler extends DefaultHandler { 
    private static final String TAG = "In Handler"; 
    String elementValue = null; 
    Boolean elementOn = false; 
    public static XMLGettersSetters data = null; 

    public static XMLGettersSetters getXMLData() { 
     return data; 
    } 

    public static void setXMLData(XMLGettersSetters data) { 
     XMLHandler.data = data; 
    } 

    /** 
    * This will be called when the tags of the XML starts. 
    **/ 
    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 

     elementOn = true; 
     Log.i(TAG, "starting Element "+localName); 
     if (localName.equals("livefeed")) 
     { 
      data = new XMLGettersSetters(); 
     } else if (localName.equals("routecount")) { 
      /** 
      * We can get the values of attributes for eg. if the CD tag had an attribute(<CD attr= "band">Akon</CD>) 
      * we can get the value "band". Below is an example of how to achieve this. 
      * 
      * String attributeValue = attributes.getValue("attr"); 
      * data.setAttribute(attributeValue); 
      * 
      * */ 
     } 
    } 

    /** 
    * This will be called when the tags of the XML end. 
    **/ 
    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 

     elementOn = false; 

     /** 
     * Sets the values after retrieving the values from the XML tags 
     * */ 
     if (localName.equalsIgnoreCase("routecount")) 
      data.set(elementValue); 
     /*else if (localName.equalsIgnoreCae("artist")) 
      data.setArtist(elementValue); 
     else if (localName.equalsIgnoreCase("country")) 
      data.setCountry(elementValue); 
     else if (localName.equalsIgnoreCase("company")) 
      data.setCompany(elementValue); 
     else if (localName.equalsIgnoreCase("price")) 
      data.setPrice(elementValue); 
     else if (localName.equalsIgnoreCase("year")) 
      data.setYear(elementValue);*/ 
    } 

    /** 
    * This is called to get the tags value 
    **/ 
    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 

     if (elementOn) { 
      elementValue = new String(ch, start, length); 
      elementOn = false; 
     } 

    } 

} 

그리고 이것은 내 GetterSetter 클래스

package com.vint.michiganbus; 

import java.util.ArrayList; 

public class XMLGettersSetters { 
    private ArrayList<String> routecount = new ArrayList<String>(); 

    public ArrayList<String> get() { 
     return routecount; 
    } 
    public void set(String company) { 
      this.routecount.add(company); 
    } 
} 

어떤 도움이 크게 감사 것입니다!

ps : URL에있는 XML을 효율적으로 처리하는 방법에 대한 제안이 있다면 누구나 저도 좋아할 것입니다!

+0

여기 내 대답을보십시오 : http://stackoverflow.com/a/10682536/1321873 – Rajesh

답변

0

활동 스레드에서 웹 파일을 가져 오는 중 오류가 발생했을 수 있습니다. AsyncTask를 만들고, 모든 파서 코드를 AsyncTask의 doInBackground() 메서드에 넣습니다. 같을 것이다 뭔가 :

private class DownloadFilesTask extends AsyncTask<Document, Integer, Document> 
{ 
    Document doc; 
    String xml; 

    public DownloadFilesTask(){} 

    protected Document doInBackground(Document... params) 
    { 
     xml = xmlFunctions.getXML("Your URL"); 
     doc = XmlFunctions.XMLfromString(xml); 
     return doc; 
    } 
    protected void onPostExecute(Document result) 
    { 
     //Some code changing UI from the nodes you've just parsed 
    } 
} 

다음 사용하여에서 onCreate이 클래스를 호출 할 수

new DownloadTaskfiles().execute(); 

희망이 도움입니다!