Skip to content Skip to sidebar Skip to footer

How To Display Image In Grid View Reading Imageurl From Xml Using Sax Parser In Android

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

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

publicclassSAXParserExampleextendsDefaultHandler{

    List myEmpls;

    private String tempVal;

    //to maintain contextprivate Employee tempEmp;


    publicSAXParserExample(){
        myEmpls = newArrayList();
    }

    publicvoidrunExample() {
        parseDocument();
        printData();
    }

    privatevoidparseDocument() {

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

            //get a new instance of parserSAXParsersp= 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
     */privatevoidprintData(){

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

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


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


    publicvoidcharacters(char[] ch, int start, int length)throws SAXException {
        tempVal = newString(ch,start,length);
    }

    publicvoidendElement(String uri, String localName, String qName)throws SAXException {

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

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

    }

    publicstaticvoidmain(String[] args){
        SAXParserExamplespe=newSAXParserExample();
        spe.runExample();
    }

}

Solution 2:

Post a Comment for "How To Display Image In Grid View Reading Imageurl From Xml Using Sax Parser In Android"