gpt4 book ai didi

ios - 具有多个连接/提要/ View 的 XML 解析的设计/实现建议

转载 作者:可可西里 更新时间:2023-11-01 03:31:26 25 4
gpt4 key购买 nike

开始我的第一个 iOS 项目,想就如何构建应用程序提出建议。该应用程序提取一个 XML 提要,将其解析出来并显示一个表示 XML 提要中项目的列表。单击列表中的项目时,应用程序将使用先前提取的 XML 提要中的属性之一提取新的 XML 提要。这种情况发生在拉取、解析、显示和用户选择的几层上,一遍又一遍地做同样的事情。现在大多数 XML 元素结构是这样的:

(这些都是简单的例子,只是为了演示发生了什么)

返回(在新 View 上显示信息):

<items>
<item id="123" name="item 1" />
<item id="124" name="item 2" />
<item id="125" name="item 3" />
</itmes>

返回:

<itemDescription>
<description itemId="123" name="desc 1" description="blah 1" />
</itemDescription>

想知道:

  • 我应该在每个 View 中有一个连接类/对象还是一个新连接?
  • 我应该在每个 View 中使用解析器类/对象还是解析 XML 提要?
  • 我还想存储一些返回的数据,这样我就不需要在用户导航回主项目列表时再次调用 XML 提要,但我需要每次都解析 itemsDescription XML 提要.

我看过一些关于解析 XML 的教程,并且了解了如何执行此操作的要点,希望更多地关注设计和可重用性,而不是在每个新 View 中重复代码。还是我对它的工作原理还差得远

最佳答案

按照 Apple 指南执行此操作的最佳方法是检查他们的示例之一,几个月前我按照这个示例制作了一个类似于您的应用程序。您还可以了解如何在离线模式下制作您的应用。

Basic structure (没有离线模式):

The SeismicXML sample application demonstrates how to use NSXMLParser to parse XML data. When you launch the application it downloads and parses an RSS feed from the United States Geological Survey (USGS) that provides data on recent earthquakes around the world. It displays the location, date, and magnitude of each earthquake, along with a color-coded graphic that indicates the severity of the earthquake. The XML parsing occurs on a background thread using NSOperation and updates the earthquakes table view with batches of parsed objects.

Advanced structure (离线模式):

Demonstrates how to use Core Data in a multi-threaded environment, following the first recommended pattern mentioned in the Core Data Programming Guide.

Based on the SeismicXML sample, it downloads and parses an RSS feed from the United States Geological Survey (USGS) that provides data on recent earthquakes around the world. What makes this sample different is that it persistently stores earthquakes using Core Data. Each time you launch the app, it downloads new earthquake data, parses it in an NSOperation which checks for duplicates and stores newly founded earthquakes as managed objects.

For those new to Core Data, it can be helpful to compare SeismicXML sample with this sample and notice the necessary ingredients to introduce Core Data in your application.

关于 cwieland 的回答,我不会使用 ASIHTTPRequest 因为是 outdated ,所以如果你想遵循他的方法,我建议你使用 AFNetworking ,您可以在其中轻松快速地处理 XML 请求:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
XMLParser.delegate = self;
[XMLParser parse];
} failure:nil];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

关于ios - 具有多个连接/提要/ View 的 XML 解析的设计/实现建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7910423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com