问题描述
我目前只是想在的项目的节点内处理的元素。我只是专注于 标题的在这一点上为简单起见,但我发现,当它分析,我只是得到相同的元素三次。
进口的java.io.InputStream;
进口的java.net.URL;
进口的java.util.ArrayList;进口javax.xml.parsers.SAXParser中;
进口javax.xml.parsers.SAXParserFactory中;进口org.xml.sax.Attributes;
进口org.xml.sax.InputSource中;
进口org.xml.sax.SAXException;
进口org.xml.sax.XMLReader中;
进口org.xml.sax.helpers.DefaultHandler中;进口android.util.Log;公共类XMLHelper扩展的DefaultHandler {
私人字符串URL_Main =http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss;
字符串标记=XMLHelper; 布尔currTag = FALSE;
串currTagVal =; 公众的ItemData项目= NULL;
公众的ArrayList<&的ItemData GT;项目=新的ArrayList<&的ItemData GT;(); 公共无效的get(){
尝试{
工厂SAXParserFactory的= SAXParserFactory.newInstance();
的SAXParser解析器= factory.newSAXParser();
的XMLReader读者= parser.getXMLReader();
reader.setContentHandler(本);
的InputStream的InputStream =新的URL(URL_Main).openStream();
reader.parse(新的InputSource(InputStream的));
}赶上(例外五){
Log.e(TAG,异常:+ e.getMessage());
}
} //接收一个元素的开始的通知 公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
属性属性)抛出的SAXException { Log.i(TAG,TAG:+的localName); currTag = TRUE;
currTagVal =;
如果(localName.equals(通道)){
项目=新的ItemData();
} } //接收元素结束的通知 公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
抛出的SAXException { currTag = FALSE; 如果(localName.equalsIgnoreCase(标题))
item.setTitle(currTagVal);
否则如果(localName.equalsIgnoreCase(项目))
items.add(项目);
} //接收元素内字符数据的通知 公共无效字符(字符[]通道,诠释开始,诠释长度)
抛出的SAXException { 如果(currTag){
currTagVal = currTagVal +新的String(CH,开始,长度); currTag = FALSE;
} }
}
Remodify整个项目,则需要3类:
1.ItemList
2.XMLHandler扩展默认的处理程序
3.SAXParsing活动
请您code举办第一
在您XMLHandler类扩展默认处理您的code应该像
公共类MyXMLHandler扩展的DefaultHandler
{
公共静态ITEMLIST ITEMLIST;
公共布尔电流= FALSE;
公共字符串CurrentValue的= NULL;@覆盖
公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
属性属性)抛出的SAXException {
// TODO自动生成方法存根 当前= TRUE; 如果(localName.equals(通道))
{
/ ** *启动/
ITEMLIST =新ITEMLIST(); }
}@覆盖
公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
抛出的SAXException {
// TODO自动生成方法存根
电流= FALSE; 如果(localName.equals(项目))
{
itemList.setItem(CurrentValue的);
}
否则,如果(localName.equals(标题))
{
itemList.setManufacturer(CurrentValue的);
}}@覆盖
公共无效字符(字符[]通道,诠释开始,诠释长度)
抛出的SAXException {
// TODO自动生成方法存根 如果(当前)
{
CurrentValue的=新的String(CH,开始,长度);
电流= FALSE;
}
}
}
ITEMLIST类是用来设置,setter和getter方法在ArrayList的价值传递和检索这些数组列表中的SAXParsing活动。
我希望这解决方案可帮助。 :D
I am currently just trying to process the elements within the item nodes. I am just focusing on the title at this point for simplicity, but I am finding that when it parses, I am just getting the same element three times.
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XMLHelper extends DefaultHandler {
private String URL_Main="http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss";
String TAG = "XMLHelper";
Boolean currTag = false;
String currTagVal = "";
public ItemData item = null;
public ArrayList<ItemData> items = new ArrayList<ItemData>();
public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(URL_Main).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Receives notification of the start of an element
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG: " + localName);
currTag = true;
currTagVal = "";
if (localName.equals("channel")) {
item = new ItemData();
}
}
// Receives notification of end of element
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;
if (localName.equalsIgnoreCase("title"))
item.setTitle(currTagVal);
else if (localName.equalsIgnoreCase("item"))
items.add(item);
}
// Receives notification of character data inside an element
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}
}
Remodify your whole project, you need 3 classes :
1.ItemList2.XMLHandler extends Default handler3.SAXParsing activity
Make your code organized first
In your XMLHandler class extend default handler your code should look like
public class MyXMLHandler extends DefaultHandler
{
public static ItemList itemList;
public boolean current = false;
public String currentValue = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
current = true;
if (localName.equals("channel"))
{
/** Start */
itemList = new ItemList();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
current = false;
if(localName.equals("item"))
{
itemList.setItem(currentValue);
}
else if(localName.equals("title"))
{
itemList.setManufacturer(currentValue);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if(current)
{
currentValue = new String(ch, start, length);
current=false;
}
}
}
ItemList class is used to set , setter and getter methods to pass in values of arraylist and retrieve those array lists in the SAXParsing activity.
I hope this solution helps. :D
这篇关于Android版的SAXParser,ArrayList中重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!