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

发布于 2024-11-17 14:40:16 字数 882 浏览 0 评论 0原文

我是安卓新手。我想创建一个应用程序来从 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-11-24 14:40:16

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

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

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

SAX Parser to parse an XML 来解析 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();
    }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文