简体   繁体   English

我可以使用SAX解析器快速获取指定标记的值吗?

[英]Can I get the value of a specified tag quickly with SAX parser?

I want to make a progress bar while importing my articles from an XML feed. 我想在从XML Feed导入文章时创建一个进度条。

The parsing works fine, but for my progress bar I need to quickly know the total # of <item> s in the feed so I can determine percentage that have been loaded. 解析工作正常,但对于我的进度条,我需要快速了解Feed中<item>的总数#,这样我就可以确定已加载的百分比。

My thought was, it would be a lot faster to just do this in PHP and add the "count" to the feed itself - something like this: 我的想法是,在PHP中执行此操作要快得多,并将“count”添加到feed本身 - 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<channel>
<title>My Apps Feed</title>
<link>http://link_to_this_fiel</link>
<language>en-us</language>
<count>42</count>

But then I need to be able to quickly access that "count" number. 但后来我需要能够快速访问“计数”号码。

At the moment, I have an RSSHandler.java that's being called like this: 目前,我有一个RSSHandler.java ,它被调用如下:

//Add all items from the parsed XML
for(NewsItem item : parser.getParsedItems())
{
    //...

Note: Min API level 8 for my app 注意:我的应用的最低API级别8

You can use Xpath to get specific node value in XML. 您可以使用Xpath以XML格式获取特定节点值。 Sample would be: 样本将是:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(.....); // get document for your xml here
Element elem = (Element) XPath.selectSingleNode(doc, "channel/count");
System.out.println(elem.getValue());

This way you can directly get count value. 这样你就可以直接获得计数值。 But I'm not sure this is the efficient and faster way to do this. 但我不确定这是否是有效且快捷的方法。 As an option you can use this. 作为选项,您可以使用它。 Also spend some time in reading this: SAX parsing - efficient way to get text nodes 还花一些时间阅读: SAX解析 - 获取文本节点的有效方法

SAX reads the tags in the order they come in. Make sure to put your tag at the beginning of the XML, otherwise you will need to parse it all anyways. SAX按照它们进入的顺序读取标记。确保将标记放在XML的开头,否则无论如何都需要解析它。 For easier parsing, I'd put it into an attribute of a self-closing tag with a unique name. 为了便于解析,我将其放入具有唯一名称的自闭标签的属性中。 Then you wait until the SAX parser calls your startElement method, check if the tag name matches the name of your count tag, extract the attribute, and display it to the user. 然后等到SAX解析器调用startElement方法,检查标记名称是否与count标记的名称匹配,提取属性,并将其显示给用户。

If you want to stop the parser after displaying the count, you can throw a SAXException to do so. 如果要在显示计数后停止解析器,可以抛出SAXException来执行此操作。

I assume you already know how to do the parsing work off the main thread, as you mention a progress bar and imply that parsing can take some time (and doing long tasks on the main thread gives you ANRs). 我假设您已经知道如何从主线程执行解析工作,因为您提到了一个进度条并暗示解析可能需要一些时间(并且在主线程上执行长任务会为您提供ANR)。 In case someone stumbles upon this question who doesn't know: You can use AsyncTask and the publishProgress (called in the "worker" thread by your code) and onProgressUpdate (called by Android on the UI thread once you call publishProgress ) methods to take care of that. 如果有人偶然发现了这个问题谁不知道:您可以使用AsyncTaskpublishProgress (在您的代码中调用“worker”线程)和onProgressUpdate (一旦调用publishProgress ,由UI线程上的Android调用)方法照顾那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM