java - Android XML (RSS) 忽略引号 (")

标签 java android xml rss xml-parsing

我在使用 SAX XML 解析器时遇到问题。 它解析除引号 (") 之外的所有内容。 例如,如果节点中的文本为 hell"3o,则结果为 hell。

这是我的代码: 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 类:

公共(public)类 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;
}
}

我忘记了另一个类(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;
}
}

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

最佳答案

字符方法可以被调用多次。尝试查看其中的内容并尝试在字符串缓冲区中累积值

关于java - Android XML (RSS) 忽略引号 ("),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685412/

相关文章:

android - Dalvik 字节码跟踪器

xml - xpath 轴中的命名空间是什么

python - Python 中的另一个 unicode 困惑

java - Scala: "val"作为标识符可能吗?链接到 java 库需要它

java - 在 Android 上检测 ui 线程在哪里停滞的简单方法?

java - Guava eventbus 调度错误

java - 如何将 spring bean 集成从 XML 转换为基于 Java 注解的 Config

java - 子类的构造函数

android - Jetpack Compose – 我需要使用哪个依赖项才能使用状态委托(delegate)?

java - 使用 libGDX 在 Java 中创建对话