Parse XML不会通过重复标题来正确加载内容

时间:2015-01-13 20:28:54

标签: java android xml

我正在创建我的XML Reader应用程序,我试图解析XDA-Developers站点提要中的内容。问题在于它不断重复第一行,也给了我一些错误。

我正在使用此sample。该示例中的链接工作正常,但是当我放置链接时,它有时会给我一个错误。

我的XMLParser:

public class XMLParserXDA extends DefaultHandler{

    private String URL_MAIN = "http://www.xda-developers.com/feed/?format=xml";
    String TAG = "XMLHelper";

    Boolean currTag = false;
    String currTagVal = "";
    private XDAObject post = null;
    private ArrayList<XDAObject> posts = new ArrayList<>();

    public ArrayList<XDAObject> getPostsList() {
        return this.posts;
    }

    public void get() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);
            InputStream mInputStream = new URL(URL_MAIN).openStream();
            mXmlReader.parse(new InputSource(mInputStream));
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (currTag) {
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currTag = false;

        if (localName.equalsIgnoreCase("title")) {
            post.setTitle(currTagVal);
        }

        else if (localName.equalsIgnoreCase("description")) {
            post.setDescription(currTagVal);
        }

        else if (localName.equalsIgnoreCase("guid")) {
            post.setGuid(currTagVal);
        }

        else if (localName.equalsIgnoreCase("item")) {
            posts.add(post);
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        Log.i(TAG, "START TAG: " + localName);

        currTag = true;
        currTagVal = "";
        if (localName.equals("item"))
            post = new XDAObject();
    }

}

问题在于:

01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: rss
01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: channel
01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: title
01-13 20:22:08.052    4274-4294/com.kostas.myapplication E/XMLHelper﹕ Exception: Attempt to invoke virtual method 'void activityobjects.XDAObject.setTitle(java.lang.String)' on a null object reference
01-13 20:22:09.103    4274-4289/com.kostas.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-13 20:22:09.103    4274-4289/com.kostas.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa36c8e80, error=EGL_SUCCESS
01-13 20:22:09.180    4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 102 frames!  The application may be doing too much work on its main thread.
01-13 20:22:12.469    4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 197 frames!  The application may be doing too much work on its main thread.

XDAObject类是:

public class XDAObject {
    private String title = null;
    private String guid = "";
    private String description = "";
    private String content = "";


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String link) {
        this.guid = link;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

任何想法?在此先感谢!!

2 个答案:

答案 0 :(得分:0)

currtagval =“”=没有任何字符串和覆盖使用参数no的方法字符

所以currtagval = currtagval + characters(intchar [] ch,int start,int length)

=

currtagvall =“”+未使用的方法==&gt;空?

答案 1 :(得分:0)

我应该修改一下:

@Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        Log.i(TAG, "START TAG: " + localName);

        currTag = true;
        currTagVal = "";
        if (localName.equals("title"))
            post = new XDAObject();
    }

标题是起始标记,而不是我拥有的项目。