【问题标题】:how to display image in grid view reading imageUrl from xml using sax parser in android如何在 android 中使用 sax 解析器从 xml 读取 imageUrl 的网格视图中显示图像
【发布时间】:2011-06-30 06:16:00
【问题描述】:

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


感谢您的回答,但我可以从 url 读取 xml 文件,但我需要在 xml imageUrl 中是否存在,以便在网格视图中显示。

这是我的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>

【问题讨论】:

    标签: android parsing sax


    【解决方案1】:

    查看以下网址了解 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();
        }
    
    }
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2011-04-30
      相关资源
      最近更新 更多