Android Sax解析器使用了错误的标记
下面是我的Saxparser类,它检索标记名“item”中的所有元素Android Sax解析器使用了错误的标记,android,xml-parsing,saxparser,Android,Xml Parsing,Saxparser,下面是我的Saxparser类,它检索标记名“item”中的所有元素 类SAXHelper{ public HashMap userList=new HashMap(); 专用URL url2; public SAXHelper(字符串url1)引发错误的DurLexException{ this.url2=新URL(url1); } 公共RSSHandler parseContent(字符串parseContent){ RSSHandler df=新的RSSHandler(); 试一试{ SA
类SAXHelper{
public HashMap userList=new HashMap();
专用URL url2;
public SAXHelper(字符串url1)引发错误的DurLexException{
this.url2=新URL(url1);
}
公共RSSHandler parseContent(字符串parseContent){
RSSHandler df=新的RSSHandler();
试一试{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
xr.setContentHandler(df);
parse(新的InputSource(url2.openStream());
}捕获(例外e){
e、 printStackTrace();
}
返回df;
}
}
类RSSHandler扩展了DefaultHandler{
private Post currentPost=新Post();
StringBuffer chars=新的StringBuffer();
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、,
属性(atts){
chars=新的StringBuffer();
if(localName.equalsIgnoreCase(“项”)){
}
}
@凌驾
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
if(localName.equalsIgnoreCase(“标题”)
&¤tPost.getTitle()=空){
currentPost.setTitle(chars.toString());
System.out.println(“title1:+currentPost.getTitle());
}
if(localName.equalsIgnoreCase(“类别”)
&¤tPost.getCategory()=空){
currentPost.setCategory(chars.toString());
System.out.println(“类别:+currentPost.getCategory());
}
if(localName.equalsIgnoreCase(“说明”)
&¤tPost.getDescription()=空){
currentPost.setDescription(chars.toString());
}
if(localName.equalsIgnoreCase(“链接”)
&¤tPost.getLink()=空){
currentPost.setLink(chars.toString());
System.out.println(“链接:+currentPost.getLink());
}
if(localName.equalsIgnoreCase(“pubDate”)
&¤tPost.getPubDate()=空){
currentPost.setPubDate(chars.toString());
字符串x=currentPost.getPubDate();
字符串last=x.substring(0,x.length()-4);
int start=last.length()-8;
int end=last.length()-3;
字符串结果=x.子字符串(开始、结束);
结果=替换(结果);
}
if(localName.equalsIgnoreCase(“项”)){
PostListNormal.add(currentPost);
currentPost=新的Post();
}
}
@凌驾
公共无效字符(字符ch[],整数开始,整数长度){
append(新字符串(ch,start,length));
}
}
下面是要提取的xml标记
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>matches en direct</title>
<link>http://www.match.com/</link>
<atom:link href="http://www.match.com/news,60,0,UTF-8,fulltext.rss" rel="self" type="application/rss+xml" />
<description>Foot 01 - N°1 de l'actu du match, du mercato et des matches en direct</description>
<language>fr-FR</language>
<pubDate>Tue, 11 Mar 2014 06:12:38 GMT</pubDate>
<lastBuildDate>Tue, 11 Mar 2014 06:12:38 GMT</lastBuildDate>
<category>Sports</category>
<copyright>© 2011 match.com</copyright>
<ttl>1</ttl>
<item>
<category>Espagne, match </category>
<title>a</title>
<link>aaaa</link>
<guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
<description>
match progress
</description>
<pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
</item>
<item>
<category>x, match </category>
<title>a</title>
<link>aaaa</link>
<guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
<description>
match progress
</description>
<pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
</item>
<item>
<category>x, match </category>
<title>a</title>
<link>aaaa</link>
<guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
<description>
match progress
</description>
<pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
</item>
<item>
<category>x, match </category>
<title>a</title>
<link>aaaa</link>
<guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
<description>
match progress
</description>
<pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
</item>
</channel>
</rss>
直接匹配
http://www.match.com/
足部01-比赛时不超过1度,直接比赛时不超过1度
法语
2014年3月11日星期二06:12:38 GMT
2014年3月11日星期二06:12:38 GMT
体育
©2011 match.com
1.
埃斯帕涅,比赛
A.
aaaa
http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131
比赛进度
2014年3月10日星期一22:52:02 GMT
x、 匹配
A.
aaaa
http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131
比赛进度
2014年3月10日星期一22:52:02 GMT
x、 匹配
A.
aaaa
http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131
比赛进度
2014年3月10日星期一22:52:02 GMT
x、 匹配
A.
aaaa
http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131
比赛进度
2014年3月10日星期一22:52:02 GMT
当我运行代码时,我的结果是标题打印的标签
直接匹配,
A.
A.
a
预期结果应为4个“a”元素
简而言之,先打印频道标签中的标题,然后忽略第一个“项目”标签,最后打印第二个标签项目之后的所有标签项目
如何打印所有标签项?只有任何想法。对不起,我的英语不好 维护一个
布尔值
,以识别项
中的当前元素,如下所示
class RSSHandler extends DefaultHandler {
private Post currentPost = new Post();
StringBuffer chars = new StringBuffer();
boolean isItem = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("item")) {
isItem = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("title") && currentPost.getTitle() == null && isItem == true) {
currentPost.setTitle(chars.toString());
System.out.println("title1: " + currentPost.getTitle());
}
if (localName.equalsIgnoreCase("category") && currentPost.getCategory() == null) {
currentPost.setCategory(chars.toString());
System.out.println("category: " + currentPost.getCategory());
}
if (localName.equalsIgnoreCase("description") && currentPost.getDescription() == null) {
currentPost.setDescription(chars.toString());
}
if (localName.equalsIgnoreCase("link") && currentPost.getLink() == null) {
currentPost.setLink(chars.toString());
System.out.println("link: " + currentPost.getLink());
}
if (localName.equalsIgnoreCase("pubDate") && currentPost.getPubDate() == null) {
currentPost.setPubDate(chars.toString());
String x = currentPost.getPubDate();
String last = x.substring(0, x.length() - 4);
int start = last.length() - 8;
int end = last.length() - 3;
String result = x.substring(start, end);
result = Stringreplace(result);
}
if (localName.equalsIgnoreCase("item")) {
PostListNormal.add(currentPost);
currentPost = new Post();
isItem = false;
}
}
@Override
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
}
所以,您的问题是,解析器应该解析4个title元素,但它解析3个title元素d…对吗?它应该只传递“item”标记中的所有标记元素,而忽略通道中的独立标记元素。用这个类替换整个
RSSHandler
类…然后说,“行不行呢?”迪米特里……很高兴听到你这么说……那么,请以赞成票接受答案。:)是的,但最后一个问题是,假设我想打印描述值,它会同时从频道获取描述,还是只从预期结果的项目中获取描述。代码是只处理title还是处理所有其他标记,因为我从日志中的描述中获取值。谢谢:)如果您没有对布尔值应用相同的条件,则将解析频道的说明
。应用与endElement()中的标题
相同的条件。
class RSSHandler extends DefaultHandler {
private Post currentPost = new Post();
StringBuffer chars = new StringBuffer();
boolean isItem = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("item")) {
isItem = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("title") && currentPost.getTitle() == null && isItem == true) {
currentPost.setTitle(chars.toString());
System.out.println("title1: " + currentPost.getTitle());
}
if (localName.equalsIgnoreCase("category") && currentPost.getCategory() == null) {
currentPost.setCategory(chars.toString());
System.out.println("category: " + currentPost.getCategory());
}
if (localName.equalsIgnoreCase("description") && currentPost.getDescription() == null) {
currentPost.setDescription(chars.toString());
}
if (localName.equalsIgnoreCase("link") && currentPost.getLink() == null) {
currentPost.setLink(chars.toString());
System.out.println("link: " + currentPost.getLink());
}
if (localName.equalsIgnoreCase("pubDate") && currentPost.getPubDate() == null) {
currentPost.setPubDate(chars.toString());
String x = currentPost.getPubDate();
String last = x.substring(0, x.length() - 4);
int start = last.length() - 8;
int end = last.length() - 3;
String result = x.substring(start, end);
result = Stringreplace(result);
}
if (localName.equalsIgnoreCase("item")) {
PostListNormal.add(currentPost);
currentPost = new Post();
isItem = false;
}
}
@Override
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
}