2012-05-21 108 views
0

我想爲Android製作一個SAX解析器,並從外部XML文件中只讀取一個標記開始。我得到一個'不幸的是,已經停止'的錯誤。我看着日誌文件,它給了我一個空引用錯誤。我的猜測是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

錯誤可能是您正嘗試從活動線程檢索Web文件。我將創建一個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(); 

希望這是一些幫助!