SAX解析器数据中的问题未加载到特定标记中

时间:2014-05-09 11:35:36

标签: android saxparser

你好朋友我有一个微调器适配器,它包含来自web服务的数据,我填写如下

Main.Java

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

}

}

当我运行上面的代码时,它只给出描述GENERAL。其他文字没有得到任何想法如何解决它?

1 个答案:

答案 0 :(得分:0)

@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 **

中添加此行