在我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)
「FeedsXMLHandler.java」中的行數** 83 **是什麼? – Hariharan
您可以發佈一個您想要解析的RSS提要的示例嗎?也許有- 元素,你不希望找到它們。 –
TofferJ
@Tamilan:行號83是 data.setTitle(bufferTitle.toString()); (endElement(...)方法) – Kunu