how to display image in grid view reading imageUrl from xml using sax parser in android

◇◆丶佛笑我妖孽 提交于 2019-12-01 10:40:50

问题


I am new in android. I want to create a application to read the XML file from an URL and show the image in a grid view using ImageUrl of image.


Thanks for the answer but I am able to read xml file from url but I need if in xml imageUrl is there so show in grid view.

This is my xml file

<?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>

回答1:


Check the following URl to kow about XML parsers

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

First get the data from url. store in in file. use the folowing code to parse the XML using SAXParser

SAX Parser to parse an XML

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

}



回答2:


  • Working with XML on Android
  • Grid View example


来源:https://stackoverflow.com/questions/6530659/how-to-display-image-in-grid-view-reading-imageurl-from-xml-using-sax-parser-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!