2011-10-07 73 views
0

我遇到了SAX XML解析器的问题 它会解析除引号(”)之外的所有内容。 例如,如果文本是地狱" 3o在一个节点,结果是地狱。Android XML(RSS)忽略引号(“)

这里是我的代码: XML处理程序:

public class MyXMLHandler extends DefaultHandler { 

Boolean currentElement = false; 
String currentValue = null; 
public static SitesList sitesList = null; 

public static SitesList getSitesList() { 
return sitesList; 
} 

public static void setSitesList(SitesList sitesList) { 
MyXMLHandler.sitesList = sitesList; 
} 

/** Called when tag starts (ex:- <name>AndroidPeople</name> 
* -- <name>)*/ 
@Override 
public void startElement(String uri, String localName, String qName, 
Attributes attributes) throws SAXException { 

currentElement = true; 

if (localName.equals("channel")) 
{ 
/** Start */ 
sitesList = new SitesList(); 
} else if (localName.equals("item")) { 
    String attr=attributes.getValue("item"); 
    sitesList.setItem(attr); 
} else if (localName.equals("title")) { 
/** Get attribute value */ 
String attr = attributes.getValue("title"); 
sitesList.setTitle(attr); 
} 
else if (localName.equals("link")) { 
/** Get attribute value */ 
String attr = attributes.getValue("link"); 
sitesList.setLink(attr); 
} 
else if (localName.equals("description")) { 
/** Get attribute value */ 
String attr = attributes.getValue("description"); 
sitesList.setDescription(attr); 
} 
else if (localName.equalsIgnoreCase("pubDate")) { 
/** Get attribute value */ 
String attr = attributes.getValue("pubDate"); 
sitesList.setPubDate(attr); 
} 

} 

/** Called when tag closing (ex:- <name>AndroidPeople</name> 
* -- </name>)*/ 
@Override 
public void endElement(String uri, String localName, String qName) 
throws SAXException { 

currentElement = false; 

/** set value */ 
if (localName.equalsIgnoreCase("item")) 
sitesList.setItem(currentValue); 
else if (localName.equalsIgnoreCase("title")) 
sitesList.setTitle(currentValue); 
else if (localName.equalsIgnoreCase("link")) 
sitesList.setLink(currentValue); 
else if (localName.equalsIgnoreCase("description")) 
sitesList.setDescription(currentValue); 
else if (localName.equalsIgnoreCase("pubDate")) 
sitesList.setPubDate(currentValue); 

} 

/** Called to get tag characters (ex:- <name>AndroidPeople</name> 
* -- to get AndroidPeople Character) */ 
@Override 
public void characters(char[] ch, int start, int length) 
throws SAXException { 

if (currentElement) { 
currentValue = new String(ch, start, length); 
currentElement = false; 
} 

} 

} 

getter和setter:

import java.util.ArrayList; 

/** Contains getter and setter method for variables */ 
public class SitesList { 

/** Variables */ 
private ArrayList<String> title = new ArrayList<String>(); 
private ArrayList<String> link = new ArrayList<String>(); 
private ArrayList<String> description = new ArrayList<String>(); 
private ArrayList<String> pubDate = new ArrayList<String>(); 
private ArrayList<String> item=new ArrayList<String>(); 



/** In Setter method default it will return arraylist 
* change that to add */ 

public ArrayList<String> getTitle() { 
return title; 
} 

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

public ArrayList<String> getLink() { 
return link; 
} 

public void setLink(String link) { 
this.link.add(link); 
} 

public ArrayList<String> getDescription() { 
return description; 
} 

public void setDescription(String description) { 
this.description.add(description); 
} 
public ArrayList<String> getPubDate() { 
    return this.pubDate; 
} 
public void setPubDate(String PubDate) { 
    this.pubDate.add(PubDate); 
} 
public ArrayList<String> getItem() { 
    return this.item; 
} 
public void setItem(String item) { 
    this.item.add(item); 
} 



} 

而且RSS Thread类:

公共类RssThread {

private String title,html,pubDate; 
public RssThread(String title,String html,String pubDate) 
{ 
    this.title=title; 
    this.html=html; 
    this.pubDate=CovertToDate(pubDate); 
} 
private String CovertToDate(String pubDate) { 
    // TODO Auto-generated method stub 
    //Wed, 28 Sep 2011 11:40:51// 
    String newDate=""; 
    if (pubDate.substring(0,pubDate.indexOf(",")).equals("Sun")) 
     newDate+="יום ראשון"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Mon")) 
     newDate+="יום שני"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Tue")) 
     newDate+="יום שלישי"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Wed")) 
     newDate+="יום רביעי"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Thu")) 
     newDate+="יום חמישי"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Fri")) 
     newDate+="יום שישי"; 
    else if (pubDate.subSequence(0, pubDate.indexOf(",")).equals("Sat")) 
     newDate+="יום שבת"; 
    newDate+=", "; 
    String[] splited = pubDate.split(" "); 
    newDate += splited[1]+"."; 
    if (splited[2].equals("Jan")) 
     newDate+="1."; 
    else if (splited[2].equals("Feb")) 
     newDate+="2."; 
    else if (splited[2].equals("Mar")) 
     newDate+="3."; 
    else if (splited[2].equals("Apr")) 
     newDate+="4."; 
    else if (splited[2].equals("May")) 
     newDate+="5."; 
    else if (splited[2].equals("Jun")) 
     newDate+="6."; 
    else if (splited[2].equals("Jul")) 
     newDate+="7."; 
    else if (splited[2].equals("Aug")) 
     newDate+="8."; 
    else if (splited[2].equals("Sep")) 
     newDate+="9."; 
    else if (splited[2].equals("Oct")) 
     newDate+="10."; 
    else if (splited[2].equals("Nov")) 
     newDate+="11."; 
    else if (splited[2].equals("Dec")) 
     newDate+="12."; 
    newDate+=splited[3]; 
    newDate+=", בשעה "+splited[4].substring(0,splited[4].lastIndexOf(":")); 
    return newDate; 

} 
public String getTitle() { 
    return this.title; 
} 
public String getHTML() { 
    return html; 
} 
public String getPubDate() { 
    return this.pubDate; 
} 
} 

我忘了把另一个类:

public class XMLParsingExample { 
    private static String[] RssString; 
/** Create Object For SiteList Class */ 
SitesList sitesList = null; 

/** Called when the activity is first created. */ 

/** Create a new textview array to display the results */ 
String[] title; 
String[] link; 
String[] pubDate; 
{ 
try { 

/** Handling XML */ 
SAXParserFactory spf = SAXParserFactory.newInstance(); 
SAXParser sp = spf.newSAXParser(); 
XMLReader xr = sp.getXMLReader(); 

/** Send URL to parse XML Tags */ 
URL sourceUrl = new URL(
"http://www.blich.co.il/rss.xml"); 

/** Create handler to handle XML Tags (extends DefaultHandler) */ 
MyXMLHandler myXMLHandler = new MyXMLHandler(); 
xr.setContentHandler(myXMLHandler); 
xr.parse(new InputSource(sourceUrl.openStream())); 

} catch (Exception e) { 
System.out.println("XML Pasing Excpetion = " + e); 
} 

/** Get result from MyXMLHandler SitlesList Object */ 
sitesList = MyXMLHandler.sitesList; 

/** Assign textview array lenght by arraylist size */ 
title = new String[sitesList.getTitle().size()]; 
link = new String[sitesList.getTitle().size()]; 
pubDate = new String[sitesList.getTitle().size()]; 

/** Set the result text in textview and add it to layout */ 
RssString=new String[sitesList.getItem().size()/2]; 
for (int i=0;i<RssString.length;i++) 
    RssString[i]=""; 
int counter=1; 
for (int i = 0; i < sitesList.getItem().size(); i++) { 
    if (i%2!=0) { 
title[i-counter]=sitesList.getTitle().get(i); 
if (title[i-counter]!=null) 
    RssString[i-counter]+=title[i-counter]+"~"; 
link[i-counter]=sitesList.getLink().get(i); 
if (link[i-counter]!=null) 
    RssString[i-counter]+=link[i-counter]+"~"; 
pubDate[i-counter]=sitesList.getPubDate().get(i); 
if (pubDate[i-counter]!=null) 
    RssString[i-counter]+=pubDate[i-counter]+"~"; 
counter++; 
    } 
} 
} 
public static String[] getRSSarray() { 
    return RssString; 
} 
} 

我给你所有的代码,所以你可以看到一切。

回答

0

也许什么都你告诉我会的工作,但它会很复杂。 我发现了一个更简单,更简单的解决方案: 其他解析器。 它使用1类(与使用3类的萨克斯解析器相比),更容易理解,当然,也不会忽略引号:D 无论如何,谢谢。

1

可以多次调用字符方法。尝试看看你在那里得到什么,并尝试积累字符串缓冲区中的值

+0

好吧,它没有帮助,它看起来像解析器只是忽略引号... –

+0

我认为你需要说服我们,解析器可以在任何地方分割文本节点,并在不同的调用中通知它。向我们展示一个完整的程序,证明它不是这样做的。 –

+0

好吧,我不能...它是一个非常复杂的程序...无论如何,相信我,它忽略了“...我可以给你一个例子:if有地狱" 3 o在一个节点中,结果是地狱...... –

-1

在分析元素之前,您是否尝试将引号转换为XML实体?

几个字符有特殊的XML实体引用:

& &amp; 
< &lt; 
> &gt; 
" &quot; 
' &apos; 
+0

不,我没有...你怎么做到这一点? –

1

您可以创建一个HTML对象,将HTML代码转换为相应的符号(即&quot;到“),然后再转换回字符串(或SpannedString如果你想格式化)

CharSequence seq = Html.fromHtml(title); 
String str = new String(seq);