读书人

XML SAX解析?失败求原因。解决办法

发布时间: 2012-04-27 11:57:44 作者: rapoo

XML SAX解析?失败,求原因。
MyDefaultHandler.java

Java code
package com.wo;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import android.util.Log;public class MyDefaultHandler extends DefaultHandler {    String tagName = null;    static News news = null;    String type = null;    @Override    public void characters(char[] ch, int start, int length)            throws SAXException {        // TODO Auto-generated method stub        if (tagName != null) {            if (type != null && type.equals("News")) {                String data = new String(ch, start, length);                if (tagName.equals("ID")) {                    news.setId(data);                    Log.v("news id", data);                } else if (tagName.equals("TITLE")) {                    news.setTitle(data);                    Log.v("news title", data);                } else if (tagName.equals("CONTENT")) {                    news.setContent(data);                    Log.v("news content", data);                } else if (tagName.equals("PUBDATE")) {                    news.setPubDate(data);                    Log.v("news pubdate", data);                }            }        }    }    @Override    public void endElement(String uri, String localName, String qName)            throws SAXException {        if (qName.equals("ITEMS")) {            if (type != null && type.equals("News")) {                Log.v("news", news.toString());            }        }        qName = null;    }    @Override    public void startElement(String uri, String localName, String qName,            Attributes attributes) throws SAXException {        if (qName.equals("ITEMS")) {            type = attributes.getValue("name");            if (type.equals("News")) {                news = new News();            } else if (type.equals("Single")) {            } else if (type.equals("House")) {            } else if (type.equals("Interview")) {[code=Java]


}
}
tagName = qName;

}

}

[/code]
News.java
Java code
package com.wo;public class News {    public String id;    public String title;    public String content;    public String pubDate;    public News() {    }    @Override    public String toString() {        return "News [id=" + id + ", title=" + title + ", content=" + content                + ", pubDate=" + pubDate + "]";    }    /**     * @return the id     */    public String getId() {        return id;    }    /**     * @param id     *            the id to set     */    public void setId(String id) {        this.id = id;    }    /**     * @return the title     */    public String getTitle() {        return title;    }    /**     * @param title     *            the title to set     */    public void setTitle(String title) {        this.title = title;    }    /**     * @return the content     */    public String getContent() {        return content;    }    /**     * @param content     *            the content to set     */    public void setContent(String content) {        this.content = content;    }    /**     * @return the pubDate     */    public String getPubDate() {        return pubDate;    }    /**     * @param pubDate     *            the pubDate to set     */    public void setPubDate(String pubDate) {        this.pubDate = pubDate;    }}

TestCursorActivity.java


Java code
package com.wo;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.lang.reflect.Field;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import android.app.Activity;import android.os.Bundle;public class TestCursorActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        DefaultHttpClient client = new DefaultHttpClient();        HttpGet request = new HttpGet("http://www.10fang.com/api/data.xml");        try {            HttpResponse response = client.execute(request);            int code = response.getStatusLine().getStatusCode();            if (code == HttpStatus.SC_OK) {                InputStream is = response.getEntity().getContent();                InputStreamReader isr = new InputStreamReader(is, "utf-8");                InputSource inputSource = new InputSource(isr);                SAXParserFactory factory = SAXParserFactory.newInstance();                SAXParser parser = factory.newSAXParser();                parser.parse(inputSource, new MyDefaultHandler());            }        } catch (ClientProtocolException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SAXException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}


[解决办法]
Java code
public BuildItem rItem;    public BuildFeed rFeed;    final int ID = 1;    final int NAME = 2;    final int ADDRESS = 3;    final int ABOUT = 4;    final int PUBDATE = 5;    private  int currentstate = 0;        public BuildFeed getFeed(){        return rFeed;    }        @Override    public void startDocument() throws SAXException {        rFeed=new BuildFeed();        rItem=new BuildItem();        super.startDocument();    }        @Override    public void endDocument() throws SAXException {        // TODO Auto-generated method stub        super.endDocument();    }        @Override    public void startElement(String uri, String localName, String qName,            Attributes attributes) throws SAXException {        // TODO Auto-generated method stub        if(localName.endsWith("channel")){             currentstate = 0;             return;        }        if(localName.endsWith("item")){            rItem=new BuildItem();            return;        }        if(localName.equals("id")){            currentstate = ID;            return;        }        if(localName.equals("name")){            currentstate = NAME;            return;        }        if(localName.equals("address")){            currentstate = ADDRESS;            return;        }        if(localName.equals("about")){            currentstate = ABOUT;            return;        }        if(localName.equals("pubDate")){            currentstate = PUBDATE;            return;        }        currentstate = 0;        super.startElement(uri, localName, qName, attributes);    }    @Override    public void endElement(String uri, String localName, String qName)            throws SAXException {        if(localName.endsWith("item")){            rFeed.addItem(rItem);        }        super.endElement(uri, localName, qName);    }        @Override    public void characters(char[] ch, int start, int length)            throws SAXException {        // TODO Auto-generated method stub        super.characters(ch, start, length);        String theString = new String(ch, start, length);         switch(currentstate){            case ID:                rItem.setId(theString);                currentstate = 0;                break;            case NAME:                rItem.setName(theString);                currentstate = 0;                break;            case ADDRESS:                rItem.setAddress(theString);                currentstate = 0;                break;            case PUBDATE:                rItem.setPubdate(theString);                currentstate = 0;                break;            case ABOUT:                rItem.setAbout(theString);                currentstate = 0;                break;            default:                return;         }    } 

读书人网 >Android

热点推荐