在Android中SAX解析progblemAndroid、SAX、progblem

2023-09-04 06:13:03 作者:baby ?你好吊

您好我有一个Android应用程序,我想从XML文件中的数据。 我有使用SAX解析器,但是有一些问题,从这类这里给出的xml文件中获取数据,所以请给我的解决方案  使用SAX解析解析下面的XML文件

我的XML文件是在这里

 < XML版本=1.0编码=UTF-8&GT?;
< XML>
    <电影>
        <文件>
            <类型> 1< /类型>
            &LT;网址&GT; HTTP://www.mauitheatre.com/< / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ UlalenaSplashAdd.jpg&LT; /路径&GT;
            &LT;冠军&GT; UlalenaSplash&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ BaldwinBeach.mp4&LT; /路径&GT;
            &LT;冠军&GT;鲍德温海滩&LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
    &LT;电影&GT;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;网址&GT;&LT; / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ AppTeaser.mp4&LT; /路径&GT;
            &LT;冠军&GT; SlackKeyShow&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ BigBeach.mp4&LT; /路径&GT;
            &LT;冠军&GT;大滩和LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
    &LT;电影&GT;
        &LT;文件&gt;
            &LT;类型&GT; 1&LT; /类型&GT;
            &LT;网址&GT; HTTP://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea< / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ raiateaADD.jpg&LT; /路径&GT;
            &LT;冠军&GT;赖阿特亚高拉默添加&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ CharleyYoungBeach.mp4&LT; /路径&GT;
            &LT;冠军&GT;查理扬海滩&LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
    &LT;电影&GT;
        &LT;文件&gt;
            &LT;类型&GT; 1&LT; /类型&GT;
            &LT;网址&GT; HTTP://www.bennyuyetake.com< / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ BennyUyetake.jpg&LT; /路径&GT;
            &LT;冠军&GT;千红Uyetake飞溅&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ HamoaBeach-1.mp4&LT; /路径&GT;
            &LT;冠军&GT;哈莫阿海滩&LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
    &LT;电影&GT;
        &LT;文件&gt;
            &LT;类型&GT; 1&LT; /类型&GT;
            &LT;网址&GT; HTTP://www.dericksebastian.com< / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ DSSplash.jpg&LT; /路径&GT;
            &LT;冠军&GT; DS飞溅&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ HanaBay.mp4&LT; /路径&GT;
            &LT;冠军&GT;花湾&LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
    &LT;电影&GT;
        &LT;文件&gt;
            &LT;类型&GT; 1&LT; /类型&GT;
            &LT;网址&GT; HTTP://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea< / URL&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ raiateaADD.jpg&LT; /路径&GT;
            &LT;冠军&GT;赖阿特亚高拉默添加&LT; /标题&GT;
        &LT; /文件&gt;
        &LT;文件&gt;
            &LT;类型&GT; 0℃/类型&GT;
            &LT;路径&GT; HTTP://64.250.238.26:1111 /短片/ KamaoleBeachPark1b-1.mp4&LT; /路径&GT;
            &LT;冠军&GT;卡玛欧力海滩公园1&LT; /标题&GT;
        &LT; /文件&gt;
    &LT; /电影&GT;
&LT; / XML&GT;
 

解决方案

假设你需要一个的ArrayList&LT;电影&GT; ,其中的电影类

 公共类电影
{
    私人的ArrayList&LT;文件&gt;文件;
    公共电影()
    {
        this.files =新的ArrayList&LT;文件&gt;();
    }
}
 

文件的结构是

 公共类文件
{
    私人诠释类型;
    私人字符串URL;
    私人字符串路径;
    私人字符串名称;
}
 
新手求助,Android Studio安装

必要的getter 二传手功能,您可以通过

获得所需的列表

 最后的SAXParser解析器= SAXParserFactory.newInstance()newSAXParser()。
最后MovieXmlHandler处理器=新MovieXmlHandler();
parser.parse(新的InputSource(新StringReader(yourXmlString)),处理程序);
最后的ArrayList&LT;电影&GT;电影= handler.getRecords();
 

其中

yourXmlString 是XML数据 您在上面粘贴,和 处理是一个MovieXmlHandler 实例。

MovieXmlHandler实现:

 公共类MovieXmlHandler扩展的DefaultHandler
{
    私有静态最后弦乐TAG_MOVIE =电影;
    私有静态最后弦乐TAG_FILE =文件;
    私有静态最后弦乐TAG_TYPE =类型;
    私有静态最后弦乐TAG_URL =URL;
    私有静态最后弦乐TAG_PATH =路径;
    私有静态最后弦乐TAG_TITLE =称号;

    私人字符串currentNodeName;
    民营电影currentMovie;
    私人文件currentFile;

    私人的ArrayList&LT;电影&GT;记录= NULL;
    私人字符串elementValue;

    公众的ArrayList&LT;电影&GT; getRecords()
    {
        返回记录;
    }

    @覆盖
    公共无效startDocument()抛出的SAXException
    {
        super.startDocument();
        this.records =新的ArrayList&LT;电影&GT;();
    }

    @覆盖
    公共无效的startElement(最后弦乐开放的,最后弦乐的localName,
            最后弦乐QNAME,最终属性ATT)抛出的SAXException
    {
        如果(的localName!= NULL)
            currentNodeName =的localName;
    }

    @覆盖
    公共无效字符(字符决赛[] CH,最终诠释开始,
            最终诠释长度)抛出的SAXException
    {
        如果(this.currentNodeName == NULL)
            返回;
        this.elementValue =新的String(CH,开始,长度).trim();

        如果(this.currentNodeName.equalsIgnoreCase(TAG_MOVIE))
            this.currentMovie =新电影();
        如果(this.currentNodeName.equalsIgnoreCase(TAG_FILE))
            this.currentFile =新的文件();
        否则,如果(this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
            this.currentFile.setType(的Integer.parseInt(this.elementValue));
        否则,如果(this.currentNodeName.equalsIgnoreCase(TAG_URL))
            this.currentFile.setUrl(this.elementValue);
        否则,如果(this.currentNodeName.equalsIgnoreCase(TAG_PATH))
            this.currentFile.setPath(this.elementValue);
        否则,如果(this.currentNodeName.equalsIgnoreCase(TAG_TITLE))
            this.currentFile.setTitle(this.elementValue);
    }

    @覆盖
    公共无效的endElement(最后弦乐开放的,最后弦乐的localName,
            最后弦乐QNAME)抛出的SAXException
    {
        如果(localName.equalsIgnoreCase(TAG_MOVIE))
        {
            如果(this.currentMovie!= NULL)
                this.records.add(this.currentMovie);
        }
        否则,如果(localName.equalsIgnoreCase(TAG_FILE))
        {
            如果((this.currentMovie = NULL)及!&安培;!(this.currentFile = NULL))
                。this.currentMovie.getFiles()加(this.currentFile);
        }
        currentNodeName = NULL;
    }
}
 

当然,如果你有你的XML数据只有URL( XMLURL:字符串),你可以使用

 最终URL sourceUrl =新的URL(XMLURL);
最后的SAXParser SP = SAXParserFactory.newInstance()newSAXParser()。
最后的XMLReader读卡器= sp.getXMLReader();
最后MovieXmlHandler处理器=新MovieXmlHandler();
reader.setContentHandler(处理);
reader.parse(新的InputSource(sourceUrl.openStream()));
最后的ArrayList&LT;电影&GT;电影= handler.getRecords();
 

如果您有可从您的XML数据中:InputStream中,然后

 最后的SAXParser解析器= SAXParserFactory.newInstance()newSAXParser()。
最后MovieXmlHandler处理器=新MovieXmlHandler();
parser.parse(在,处理程序);
最后的ArrayList&LT;电影&GT;电影= handler.getRecords();
 

让我们知道这是不是你所期待的。

Hi i have an android application and i want to get a data from xml file. i have use SAX parser but there is some problem to get data from this type of xml file given here, so please given me the solution for parse the following xml file using SAX parsing

my xml file is here

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <movie>
        <file>
            <type>1</type>
            <url>http://www.mauitheatre.com/</url>
            <path>http://64.250.238.26:1111/clips/UlalenaSplashAdd.jpg</path>
            <title>UlalenaSplash</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/BaldwinBeach.mp4</path>
            <title>Baldwin Beach</title>
        </file>
    </movie>
    <movie>
        <file>
            <type>0</type>
            <url></url>
            <path>http://64.250.238.26:1111/clips/AppTeaser.mp4</path>
            <title>SlackKeyShow</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/BigBeach.mp4</path>
            <title>Big Beach</title>
        </file>
    </movie>
    <movie>
        <file>
            <type>1</type>
            <url>http://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea</url>
            <path>http://64.250.238.26:1111/clips/raiateaADD.jpg</path>
            <title>Raiatea Keola Beamer add</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/CharleyYoungBeach.mp4</path>
            <title>Charley Young Beach</title>
        </file>
    </movie>
    <movie>
        <file>
            <type>1</type>
            <url>http://www.bennyuyetake.com</url>
            <path>http://64.250.238.26:1111/clips/BennyUyetake.jpg</path>
            <title>Benny Uyetake SPlash</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/HamoaBeach-1.mp4</path>
            <title>Hamoa Beach</title>
        </file>
    </movie>
    <movie>
        <file>
            <type>1</type>
            <url>http://www.dericksebastian.com</url>
            <path>http://64.250.238.26:1111/clips/DSSplash.jpg</path>
            <title>DS Splash</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/HanaBay.mp4</path>
            <title>Hana Bay</title>
        </file>
    </movie>
    <movie>
        <file>
            <type>1</type>
            <url>http://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea</url>
            <path>http://64.250.238.26:1111/clips/raiateaADD.jpg</path>
            <title>Raiatea Keola Beamer add</title>
        </file>
        <file>
            <type>0</type>
            <path>http://64.250.238.26:1111/clips/KamaoleBeachPark1b-1.mp4</path>
            <title>Kamaole Beach Park 1</title>
        </file>
    </movie>
</xml>

解决方案

Assuming that you need an ArrayList<Movie>, where the structure of the Movie class is

public class Movie
{
    private ArrayList<File> files;
    public Movie()
    {
        this.files = new ArrayList<File>();
    }
}

and the File's structure is

public class File
{
    private int type;
    private String url;
    private String path;
    private String title;
}

with the necessary getter and setter functions, you can get the desired list by

final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(new InputSource(new StringReader(yourXmlString)), handler);
final ArrayList<Movie> movies = handler.getRecords();

where

yourXmlString is the xml data you've pasted above, and handler is a MovieXmlHandler instance.

MovieXmlHandler implementation:

public class MovieXmlHandler extends DefaultHandler
{
    private static final String TAG_MOVIE = "movie";
    private static final String TAG_FILE = "file";
    private static final String TAG_TYPE = "type";
    private static final String TAG_URL = "url";
    private static final String TAG_PATH = "path";
    private static final String TAG_TITLE = "title";

    private String currentNodeName;
    private Movie currentMovie;
    private File currentFile;

    private ArrayList<Movie> records = null;
    private String elementValue;

    public ArrayList<Movie> getRecords()
    {
        return records;
    }

    @Override
    public void startDocument() throws SAXException
    {
        super.startDocument();
        this.records = new ArrayList<Movie>();
    }

    @Override
    public void startElement(final String Uri, final String localName, 
            final String qName, final Attributes att) throws SAXException
    {
        if (localName != null)
            currentNodeName = localName;
    }

    @Override
    public void characters(final char[] ch, final int start, 
            final int length) throws SAXException
    {
        if (this.currentNodeName == null)
            return;
        this.elementValue = new String(ch, start, length).trim();

        if (this.currentNodeName.equalsIgnoreCase(TAG_MOVIE))
            this.currentMovie = new Movie();
        if (this.currentNodeName.equalsIgnoreCase(TAG_FILE))
            this.currentFile = new File();
        else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
            this.currentFile.setType(Integer.parseInt(this.elementValue));
        else if (this.currentNodeName.equalsIgnoreCase(TAG_URL))
            this.currentFile.setUrl(this.elementValue);
        else if (this.currentNodeName.equalsIgnoreCase(TAG_PATH))
            this.currentFile.setPath(this.elementValue);
        else if (this.currentNodeName.equalsIgnoreCase(TAG_TITLE))
            this.currentFile.setTitle(this.elementValue);
    }

    @Override
    public void endElement(final String Uri, final String localName, 
            final String qName) throws SAXException
    {
        if (localName.equalsIgnoreCase(TAG_MOVIE))
        {
            if (this.currentMovie != null)
                this.records.add(this.currentMovie);
        }
        else if (localName.equalsIgnoreCase(TAG_FILE))
        {
            if ((this.currentMovie != null) && (this.currentFile != null))
                this.currentMovie.getFiles().add(this.currentFile);
        }
        currentNodeName = null;
    }
}

Of course, if you have only the url of your xml data (xmlUrl:String), you can use

final URL sourceUrl = new URL(xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final MovieXmlHandler handler = new MovieXmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
final ArrayList<Movie> movies = handler.getRecords();

and if you have your xml data available from an in:InputStream, then

final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(in, handler);
final ArrayList<Movie> movies = handler.getRecords();

Let us know if this isn't what you were looking for.