Android中SAX解析问题

2024-01-07

您好,我有一个 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>

假设您需要一个ArrayList<Movie>,其中的结构Movie类是

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

and the File的结构是

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

与必要的getter and setter函数,您可以通过以下方式获取所需的列表

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

如果您有 xml 数据可从in:InputStream, then

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android中SAX解析问题 的相关文章

  • 在Android中将半径边框绘制到imageview或textview的一个角落

    我需要在我的应用程序中为图像视图或文本视图绘制边框 但我只需要在一个角落绘制它 就像图像一样 我做了一个形状 但我在所有 4 个边上都有边框
  • Android 从键盘读取

    我的登录屏幕根本没有文本字段 当用户使用 RFID 扫描仪扫描他的 id 令牌时 我会得到一个 8 个字符长的字符串 其原理与使用键盘相同 只是更快 我希望我的登录活动在用户扫描其令牌时而不是之前执行 有一个聪明的方法来实现这个吗 我不能有
  • Android中如何将文件写入raw文件夹?

    我认为这是一个非常基本的问题 我目前正在编写这样的文件 File output new File exampleout mid 现在 我想将文件写入 myproject res raw 我读到我可以通过将完整的网址放在 中来做到这一点 但
  • Android Accessibility 执行触摸操作

    我想知道是否可以使用 Android 辅助功能服务在屏幕上的位置执行触摸操作 例如 Bundle arguments new Bundle arguments putInt coord X X value arguments putInt
  • Android相当于javascript的setTimeout和clearTimeout?

    setTimeout 有一个答案https stackoverflow com a 18381353 433570 https stackoverflow com a 18381353 433570 它没有提供我们是否可以像在 JavaSc
  • Android:拍照后调用裁剪活动

    我在解析拍摄照片的 uri 来裁剪活动时遇到问题 在我的应用程序中 用户可以拍摄一张照片或从图库中选择一张照片 然后裁剪并上传 一切听起来都很简单 从图库中选择时 图库应用程序会返回所选照片的 uri 如下所示 content media
  • 错误:任务“:app:mergeDebugResources”执行失败。 > java.lang.ArrayIndexOutOfBoundsException(无错误消息)

    你们有人知道 Gradle 构建中的这个异常吗 Error Execution failed for task app mergeDebugResources gt java lang ArrayIndexOutOfBoundsExcept
  • 如何去除 XSL 中字符的重音符号?

    我一直在寻找 但找不到相当于字符 规范化空间 的 XSL 函数 也就是说 我的内容带有重音 UNICODE 字符 这很好 但是从该内容中 我正在创建一个文件名 但我不想要这些重音 那么 是否有一些我忽略的东西 或者没有正确地谷歌搜索来轻松处
  • 如何找到特定路线上两点之间的距离?

    我正在为我的大学开发一个 Android 应用程序 可以帮助学生跟踪大学巴士的当前位置 并为他们提供巴士到达他们的预计时间 截至目前 我获取了公交车的当前位置 通过公交车上的设备 和学生的位置 我陷入了必须找到两个 GPS 坐标之间的距离的
  • 仅在 Android 应用程序中使用 XHDPI 可绘制对象?

    如果您计划在不久的将来支持 LDPI MDPI HPDI 或许还有 XHDPI 那么是否可以在项目中仅包含 XHDPI 可绘制对象并让设备将其缩放到所需的分辨率 我已经测试过在 Photoshop 中将可绘制对象的大小调整为 MDPI 和
  • JSPX 命名空间对于 EL 函数不可见?

    我正在尝试使用 JSPX JSP 的纯 XML 语法 并遇到看起来应该可以工作但实际上却不起作用的情况 我使用 jsp root 元素中的命名空间声明导入标签库 然后稍后将这些用于元素以及 EL 函数
  • twitter4j => AndroidRuntime(446): java.lang.NoClassDefFoundError: twitter4j.http.AccessToken

    我正在尝试使用 twitter4j 我的应用程序来连接并发布到 Twitter 我正在关注本教程 http blog doityourselfandroid com 2011 02 13 guide to integrating twitt
  • 从 Handler.obtainMessage() 获取什么参数

    我正在使用线程来执行一些 BT 任务 我正在尝试向 UI 线程发送消息 以便我可以基于我的 BT 线程执行 UI 工作 为此 我使用处理程序 但我不知道如何检索发送到处理程序的数据 要发送数据 我使用 handler obtainMessa
  • Android 26 (O) 通知不显示操作图标 [重复]

    这个问题在这里已经有答案了 随着 Android 26 O 引入通知渠道 我一直在调查 Google 提供的com example android notificationchannels 这个示例按预期工作 直到我尝试添加Action到示
  • Proguard - 找不到任何超级类

    我收到此错误 Unexpected error while performing partial evaluation Class org apache log4j chainsaw Main Method
  • InAppMessage 一旦显示就会自动消失

    您好 我最近将 InAppMessaging 添加到我的项目中 这似乎很容易集成 但对我来说并没有按预期工作 首先 我将其添加到 build gradle 中 implementation com google firebase fireb
  • 为什么 ExpandableListView 更改 ChildView 设置(Android)?

    我对使用 ExpandableListView 有疑问 就我而言 我有两个组视图和两个子视图 而子视图由一个带有多个按钮 文本视图等的相对布局组成 例如 当首先扩展第二组并对视图持有者进行一些更改并随后扩展第一组时 先前所做的更改也会自动应
  • 获取当前图片在图库中显示的位置

    在我的应用程序中 我有一个图片库 但我想检测当前显示图像的位置 例如 当我启动我的活动时 位置是 0 但是当我在图库中滚动时 我想获取当前显示图像的位置 我尝试过 OnFocusChanged OnItemClicked 但只有当我单击图库
  • 如何让用户在android列表视图中选择主题?

    我有一个带有两个标签的列表视图 标题和副标题 我想要深色和浅色背景作为用户选项 标题具有 textAppearanceMedium 副标题具有 textAppearanceSmall 我希望样式 MyTheme Dark 具有白色文本 My
  • 进程被杀死后不会调用 onActivityResult

    我有一个主要活动 Main 和另一个活动 Sub 由 Main 调用 startActivityForResult new Intent this SubActivity class 25 当我在 Sub 时 我终止该进程 使用任务管理器或

随机推荐