2013-12-19 78 views
1

在我MainActivity.java我已經寫代碼解析像爲什麼我要我的初始化對象兩次在解析RSS提要

public void parser() { 
    try { 
     /****** Creating a new instance of the SAX parser ****************/ 
     SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
     SAXParser saxParser = saxPF.newSAXParser(); 
     XMLReader xmlReader = saxParser.getXMLReader(); 

     URL url = new URL("http://ibnlive.in.com/ibnrss/rss/world/world.xml"); 

     myXMLHandler = new FeedsXMLHandler(); 
     xmlReader.setContentHandler(myXMLHandler); 
     xmlReader.parse(new InputSource(url.openStream()));  
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    feedsData = myXMLHandler.getXMLData(); 
} 

我使用IBN Live的RSS訂閱

我的處理程序類XMLHandler。 java的如下:

@Override 
public void startElement(String uri, String localName, String qName, 
        Attributes attributes) throws SAXException { 
    elementValue = ""; 
    elementOn = true; 

    if (localName.equalsIgnoreCase("rss")) { 
     dataArray = new ArrayList<FeedsItems>(); 
     data = new FeedsItems(); 
     Log.v("Item", "I am in rss block"); 
    } 

    if (localName.equalsIgnoreCase("channel")) { 
     Log.v("Item", "I am in channel block"); 
    } 

    if (localName.equalsIgnoreCase("item")) { 
     data = new FeedsItems(); 
     Log.v("Item", "I am in item block"); 
    } 

    if (localName.equalsIgnoreCase("description")) { 
     bufferDesc = new StringBuffer(); 
     elementOn = true; 
    } 

    if (localName.equalsIgnoreCase("title")) { 
     bufferTitle = new StringBuffer(); 
     elementOn = true; 
    } 

    if (localName.equalsIgnoreCase("link")) { 
      bufferLink = new StringBuffer(); 
      elementOn = true; 
    } 
} 

/*********** Method 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("title")) { 
     elementOn = false; 

     data.setTitle(bufferTitle.toString()); 
     bufferTitle.delete(0, bufferTitle.length()); 
     Log.v("title", data.getTitle()); 
    } 
    else if (localName.equalsIgnoreCase("link")) { 
     elementOn = false; 

     data.setFeedsUrl(bufferLink.toString()); 
     bufferLink.delete(0, bufferLink.length()); 
    } 
    else if (localName.equalsIgnoreCase("description")) { 
     elementOn = false; 

     data.setDescription(bufferDesc.toString()); 
     bufferDesc.delete(0, bufferDesc.length()); 
    } else if (localName.equalsIgnoreCase("item")) { 
     dataArray.add(data); 
    } 
} 

public void characters(char[] ch, int start, int length) throws SAXException { 
    if (elementOn) { 
     if (bufferDesc != null) { 
      bufferDesc.append(new String(ch, start, length).trim()); 
     } 

     if (bufferTitle != null) { 
      bufferTitle.append(new String(ch, start, length).trim()); 
     } 

     if (bufferLink != null) { 
      bufferLink.append(new String(ch, start, length).trim()); 
     } 

    } else { 
     elementValue = new String(ch, start, length); 
    } 
} 

問題就是爲什麼我有兩次初始化我FeedsItems一流,而在「項目」塊「RSS」一塊和第二?

如果我初始化它只有在「RSS」塊,然後按相同的數據反覆添加到我的AarrayList,這是最後一個從響應飼料項目,如果我只在「項目」塊初始化它,它給了我下面的錯誤

java.lang.NullPointerException 
at com.example.feedsdemo.FeedsXMLHandler.endElement(FeedsXMLHandler.java:83) 
at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:160) 
at org.apache.harmony.xml.ExpatParser.append(Native Method) 
at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:521) 
at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:482) 
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:320) 
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:277) 
at com.example.feedsdemo.NewsFeedsActivity.parser(NewsFeedsActivity.java:65) 
at com.example.feedsdemo.NewsFeedsActivity.onCreate(NewsFeedsActivity.java:36) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 
+0

「FeedsXMLHandler.java」中的行數** 83 **是什麼? – Hariharan

+0

您可以發佈一個您想要解析的RSS提要的示例嗎?也許有元素,你不希望找到它們。 – TofferJ

+0

@Tamilan:行號83是 data.setTitle(bufferTitle.toString()); (endElement(...)方法) – Kunu

回答

0

試試這個..

@Override 
public void startElement(String uri, String localName, String qName, 
        Attributes attributes) throws SAXException { 
    elementValue = ""; 
    elementOn = true; 

    if (localName.equalsIgnoreCase("rss")) { 
     dataArray = new ArrayList<FeedsItems>(); 
     data = new FeedsItems(); 
     bufferLink = new StringBuffer(); 
     bufferDesc = new StringBuffer(); 
     bufferTitle = new StringBuffer(); 
     Log.v("Item", "I am in rss block"); 
    }  
    else if (localName.equalsIgnoreCase("channel")) { 
     Log.v("Item", "I am in channel block"); 
    }  
    else if (localName.equalsIgnoreCase("item")) {   
     Log.v("Item", "I am in item block"); 
    }  
    else if (localName.equalsIgnoreCase("description")) {   
     elementOn = true; 
    }  
    else if (localName.equalsIgnoreCase("title")) { 
     elementOn = true; 
    }  
    else if (localName.equalsIgnoreCase("link")) {     
      elementOn = true; 
    } 
} 
0

你必須做兩次,因爲這兩個<channel><item>(也<image>)使用<title>標籤。如果你不

if (localName.equalsIgnoreCase("rss")) { 
... 
} 

instansiate它,你會得到你張貼的NullPointerException異常,當你發現中的第一個<channel>

第一<title>你想要做什麼或者是既保留或刪除一個用於「rss」,並在endElement()中添加一個檢查標題屬於<item>而不是任何其他元素。

相關問題