Friday, October 7, 2011

[android-developers] XML (RSS) Ignores quotation marks

Im having a problem with SAX XML parser.
It DOES parse everything, except quotation marks (").
For example, if the text is hell"3o in a node, the result is hell.
Here are my codes:
**XML Handler:**

    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 and 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);
    }
    
    
    
    }

And **RSS Thread class:**

public class 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;
}
}

I have forgotten to put another class:

    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;
    }
    }

ignore the foreign language xD
I gave you all the codes so you can see everything...
Thank you very much!!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment