当前位置: 代码迷 >> Android >> XML SAX解析?失败,求原因。解决办法
  详细解决方案

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

热度:63   发布时间:2016-05-01 21:27:28.0
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
  相关解决方案