【问题标题】:SAX parsing progblem in androidandroid中的SAX解析问题
【发布时间】:2011-04-20 12:43:46
【问题描述】:

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

我的 xml 文件在这里

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

【问题讨论】:

  • 能否请您粘贴错误?否则很难查明问题。
  • 也分享你的解析器实现,让我们看看那里可能出了什么问题。谢谢!

标签: android xml saxparser


【解决方案1】:

假设你需要一个ArrayList&lt;Movie&gt;,其中Movie类的结构是

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

File 的结构是

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

通过必要的gettersetter函数,你可以得到想要的列表

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

在哪里

  • yourXmlString 是xml数据 你在上面粘贴了,并且
  • handler 是一个 MovieXmlHandler 实例。

MovieXmlHandler 实现:

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

当然,如果你只有你的xml数据的url(xmlUrl:String),你可以使用

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

如果您有来自in:InputStream 的 xml 数据,那么

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

如果这不是您想要的,请告诉我们。

【讨论】:

    【解决方案2】:

    真正的问题是您自己尝试使用 SAX 解析器;这使生活变得比需要的更艰难。如果可能,为什么不依赖像 Simple XML Library 这样的基于注解的 XML 解析框架?

    我刚刚写了一个blog post on how to include it in your Android projects

    【讨论】:

      【解决方案3】:

      问题可能是&lt;xml&gt;标签没有关闭。为了能够解析该文件,它必须是一个格式良好的 XML 文件,因此请尝试在末尾添加&lt;/xml&gt;,然后再试一次。

      【讨论】:

      • xml 标记实际上已关闭,但格式不正确。
      • @rekaszeru:好的,那么我们需要更多信息;)
      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2010-12-29
      • 2012-12-26
      • 2011-09-23
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      相关资源
      最近更新 更多