android - 如何在 android 中使用 sax 解析器从 xml 读取 imageUrl 在 GridView 中显示图像

标签 android parsing sax

我是安卓新手。我想创建一个应用程序来从 URL 读取 XML 文件,并使用图像的 ImageUrl 在 GridView 中显示图像。


感谢您的回答,但我能够从 url 读取 xml 文件,但我需要在 xml 中是否存在 imageUrl,以便在 GridView 中显示。

这是我的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<channels>
    <channel>
        <name>ndtv</name>


<logo>http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png</logo>       
        <description>this is a news Channel</description>
        <rssfeed>ndtv.com</rssfeed>
    </channel>
    <channel>
        <name>star news</name>


<logo>http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png</logo>        
        <description>this is a news Channel</description>
        <rssfeed>starnews.com</rssfeed>
    </channel>
</channels>

最佳答案

检查以下 URl 以了解 XML 解析器

http://www.totheriver.com/learn/xml/xmltutorial.html#6.2

首先从url中获取数据。存储在文件中。使用以下代码使用 SAXParser 解析 XML

用于解析 XML 的 SAX 解析器

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler{

    List myEmpls;

    private String tempVal;

    //to maintain context
    private Employee tempEmp;


    public SAXParserExample(){
        myEmpls = new ArrayList();
    }

    public void runExample() {
        parseDocument();
        printData();
    }

    private void parseDocument() {

        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {

            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();

            //parse the file and also register this class for call backs
            sp.parse("employees.xml", this);

        }catch(SAXException se) {
            se.printStackTrace();
        }catch(ParserConfigurationException pce) {
            pce.printStackTrace();
        }catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    /**
     * Iterate through the list and print
     * the contents
     */
    private void printData(){

        System.out.println("No of Employees '" + myEmpls.size() + "'.");

        Iterator it = myEmpls.iterator();
        while(it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }


    //Event Handlers
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //reset
        tempVal = "";
        if(qName.equalsIgnoreCase("Employee")) {
            //create a new instance of employee
            tempEmp = new Employee();
            tempEmp.setType(attributes.getValue("type"));
        }
    }


    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch,start,length);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {

        if(qName.equalsIgnoreCase("Employee")) {
            //add it to the list
            myEmpls.add(tempEmp);

        }else if (qName.equalsIgnoreCase("Name")) {
            tempEmp.setName(tempVal);
        }else if (qName.equalsIgnoreCase("Id")) {
            tempEmp.setId(Integer.parseInt(tempVal));
        }else if (qName.equalsIgnoreCase("Age")) {
            tempEmp.setAge(Integer.parseInt(tempVal));
        }

    }

    public static void main(String[] args){
        SAXParserExample spe = new SAXParserExample();
        spe.runExample();
    }

}

关于android - 如何在 android 中使用 sax 解析器从 xml 读取 imageUrl 在 GridView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6530659/

相关文章:

android - 使用实时数据、改造、mvvm 和存储库模式制作通用网络适配器

android - “新项目”失败 : Android Studio 0. 3.4

python - 从 CSV 文件创建矩阵

javascript - 在 python 中解析 JavaScript 参数值

java - JMS 消息解析异常

android - 如何更新库 ( .aar ) 文件中的谷歌播放服务版本

python - htmlparse 无法清除 &lt;style&gt;

android - 使用 Sax 解析错误的 XML 时如何忽略 XML 错误(在 Android 上)

python - 使用 Python 的 xml.etree 查找元素开始和结束字符偏移

android - AudioRecord read() 返回奇怪的值