rss - I'm having trouble with XML parser in android -
i keep seeing tutorials doesn't help, i'm using xmlpullparser keeps getting error , application shows xmlerror page, because of format of xml?
here i'm doing:
public class xmlparser { private static final string ns = null; public queue<item> parse(inputstream in) throws xmlpullparserexception, ioexception { try { xmlpullparser parser = xml.newpullparser(); parser.setfeature(xmlpullparser.feature_process_namespaces, false); parser.setinput(in, null); parser.nexttag(); return readfeed(parser); } { in.close(); } } private queue<item> readfeed(xmlpullparser parser) throws xmlpullparserexception, ioexception { queue<item> resp = new linkedlist<item>(); parser.require(xmlpullparser.start_tag, ns, "channel"); while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); if (name.equals("item")) { resp.add(readitem(parser)); } else { skip(parser); } } return resp; } // class represents single entry (post) in xml feed. // includes data members "title," "link," , "summary." public static class item { public final string title; public final string link; public final string summary; private item(string title, string summary, string link) { this.title = title; this.summary = summary; this.link = link; } } // parses contents of entry. if encounters title, summary, or link tag, hands them // off // respective "read" methods processing. otherwise, skips tag. private item readitem(xmlpullparser parser) throws xmlpullparserexception, ioexception { parser.require(xmlpullparser.start_tag, ns, "item"); string title = null; string summary = null; string link = null; system.out.print("entrou readitem"); while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); if (name.equals("title")) { title = readtitle(parser); } else if (name.equals("description")) { summary = readdescription(parser); } else if (name.equals("link")) { link = readlink(parser); } else { skip(parser); } } return new item(title, summary, link); } // processes title tags in feed. private string readtitle(xmlpullparser parser) throws ioexception, xmlpullparserexception { system.out.print("entrou title"); parser.require(xmlpullparser.start_tag, ns, "title"); string title = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "title"); return title; } // processes link tags in feed. private string readlink(xmlpullparser parser) throws ioexception, xmlpullparserexception { parser.require(xmlpullparser.start_tag, ns, "link"); string link = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "link"); return link; } // processes summary tags in feed. private string readdescription(xmlpullparser parser) throws ioexception, xmlpullparserexception { parser.require(xmlpullparser.start_tag, ns, "description"); string summary = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "description"); return summary; } // tags title , summary, extracts text values. private string readtext(xmlpullparser parser) throws ioexception, xmlpullparserexception { string result = ""; if (parser.next() == xmlpullparser.text) { result = parser.gettext(); parser.nexttag(); } return result; } // skips tags parser isn't interested in. uses depth handle nested tags. i.e., // if next tag after start_tag isn't matching end_tag, keeps going until // finds matching end_tag (as indicated value of "depth" being 0). private void skip(xmlpullparser parser) throws xmlpullparserexception, ioexception { if (parser.geteventtype() != xmlpullparser.start_tag) { throw new illegalstateexception(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case xmlpullparser.end_tag: depth--; break; case xmlpullparser.start_tag: depth++; break; } } } }
and here feed i'm trying pull info from:
solved, think if using xmlparser have make sure accessing 1.0 or higher, when used xml 2.0 worked charm. can confirm this?
Comments
Post a Comment