Android SAX解析器数据未加载到特定标记中的问题
朋友们好,我有一个spinner适配器,其中包含来自web服务的数据,我将其填充如下 Main.JavaAndroid SAX解析器数据未加载到特定标记中的问题,android,saxparser,Android,Saxparser,朋友们好,我有一个spinner适配器,其中包含来自web服务的数据,我将其填充如下 Main.Java try { /** * Create a new instance of the SAX parser **/ SAXParserFactory saxPF = SAXParserFactory.newInstance(); SAXParser saxP = saxP
try {
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("http://www.findyourfate.com/rss/yearly-horoscope.asp?sign=Leo"); // URL of the XML
System.out.println("URL Y "+url);
/**
* Create the Handler to handle each of the XML tags.
**/
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
}
data = XMLHandler.data;
mTextViewDate.setText("Date : "+ data.getTitle());
mTextViewDesc.setText(data.getDescription());
System.out.println("data.getDescription() "+data.getDescription());
**XMLGettersSetters.java**
public class XMLGettersSetters {
String title="";
String description="" ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
XMLHandler.java
公共类XMLHandler扩展了DefaultHandler{
String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;
public static XMLGettersSetters getXMLData() {
return data;
}
public static void setXMLData(XMLGettersSetters data) {
XMLHandler.data = data;
}
/**
* This will be called when the tags of the XML starts.
**/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementOn = true;
if (localName.equals("channel"))
{
data = new XMLGettersSetters();
} else if (localName.equals("item")) {
/**
* We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> )
* we can get the value "band". Below is an example of how to achieve this.
*
* String attributeValue = attributes.getValue("attr");
* data.setAttribute(attributeValue);
*
* */
}
}
/**
* This will be called when the tags of the XML end.
**/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("title"))
data.setTitle(elementValue);
else if (localName.equalsIgnoreCase("description"))
data.setDescription(elementValue);
}
/**
* This is called to get the tags value
**/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
elementOn = false;
}
}
String elementValue=null;
布尔元素=false;
公共静态XMLGettersSetters数据=null;
公共静态XMLGettersSetters getXMLData(){
返回数据;
}
公共静态void setXMLData(XMLGettersSetters数据){
XMLHandler.data=数据;
}
/**
*这将在XML的标记启动时调用。
**/
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
elementOn=true;
if(localName.equals(“通道”))
{
数据=新的XMLGettersSetters();
}else if(localName.equals(“项”)){
/**
*我们可以得到属性的值,例如,如果CD标签有一个属性(Akon)
*我们可以得到“band”值。下面是如何实现这一点的示例。
*
*字符串attributeValue=attributes.getValue(“attr”);
*data.setAttribute(attributeValue);
*
* */
}
}
/**
*这将在XML的标记结束时调用。
**/
@凌驾
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
elementOn=false;
/**
*从XML标记检索值后设置值
* */
if(localName.equalsIgnoreCase(“标题”))
data.setTitle(elementValue);
else if(localName.equalsIgnoreCase(“说明”))
data.setDescription(元素值);
}
/**
*调用此函数以获取标记值
**/
@凌驾
公共无效字符(字符[]ch,整数开始,整数长度)
抛出SAX异常{
if(elementOn){
elementValue=新字符串(ch、开始、长度);
elementOn=false;
}
}
}
当我运行上面的代码时,它只给出一般的描述。其他文本不太明白我该如何解决它
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
sb.append(title);
sb.append('\n');
sb.append(pubdate);
sb.append(link);
sb.append('\n');
sb.append(description);
sb.append('\n');
return sb.toString().toignorecase();
}
尝试在**XMLGettersSetters.java**中添加这一行,您可以删除链接和描述标记请发送您的电子邮件id我会这样做,因为我没有阅读rssfeed
public class XMLGettersSetters {
String title="";
String description="" ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}