0

我有一个 XML 文件作为 url,其中文件包含这样的内容,

<Products>
<products id="1">
<name>product1</name>
<price>$150</price>
<img>http://www.myimage.com/Main_pic.png</img>
</products>
</Products>

现在使用 NSXMLParser 我可以检索数据和图像,但是在我获得这些数据后,我不明白如何存储在 NSMutableArray 或 NSDictionary 中。我还尝试从 Apple 的 Lazy Loading Table Images Asynchronous Downloading实现延迟加载异步图像下载。但我未能加载图像。我们有任何替代异步加载的方法吗?

请详细描述我,

  1. 如何在异步下载中存储从 url XML 文件中检索的数据。
  2. 如何分离数据和图像。
  3. 如何在 UIImage 和数据中实现这些以在 iPhone 中查看。

已编辑

为了更好地理解,我需要如下图所示的数据和图像,而不是在表格视图中。 示例图片

EDITED-2 通过使用我根据我的内容修改的lazyloading解析here代码供您参考,

- (void)main{
self.workingArray = [NSMutableArray array];
self.workingPropertyString = [NSMutableString string];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataToParse];
[parser setDelegate:self];
[parser parse];
if (![self isCancelled]){
self.completionHandler(self.workingArray);}
self.workingArray = nil;
self.workingPropertyString = nil;
self.dataToParse = nil;}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Products"]){
self.workingEntry = [[egsLists alloc] init];}
storingCharacterData = [elementsToParse containsObject:elementName];}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if (self.workingEntry){
if (storingCharacterData){
NSString *trimmedString = [workingPropertyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[workingPropertyString setString:@""];
if ([elementName isEqualToString:sIDStr]){
self.workingEntry.ProductID = trimmedString;}
else if ([elementName isEqualToString:sImageStr]){
self.workingEntry.img = trimmedString;}}
else if ([elementName isEqualToString:@"Products"]){
[self.workingArray addObject:self.workingEntry];
self.workingEntry = nil;}}}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (storingCharacterData){
[workingPropertyString appendString:string];}}
4

1 回答 1

1

上课NSObjectDataClass

// 为你的数据DataClass.h创建Synthesize对象DataClass.m

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

DataClass *mydata=[[DataClass alloc]init];

if ([elementName isEqualToString:@"id"]) 
    {
mydata.idvalue=[currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if ([elementName isEqualToString:@"name"]) 
    {
mydata.namevalue=[currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if ([elementName isEqualToString:@"price"]) 
    {
mydata.pricevalue=[currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if ([elementName isEqualToString:@"img"]) 
    {
mydata.imgLink=[currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}


[myArray addObject:mydata];



}

之后,当您需要时,如果您正在使用,您可以从数组中检索数据TableView

然后在您的CellForRowAtindexPath方法中使用以下内容:

DataClass *mydata=[[DataClass alloc]init];
mydata=[myarray objectAtindex:indexPath.row];

cell.textlabel.text=mydata.nameValue;
cell.imageView.image=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:mydata.imglink]]];

您可以使用Apple 的LazyTableImages SampleCode来加载图像。

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

如需完整的 XML 解析,请参阅教程或使用以下代码:

来源:http ://www.theappcodeblog.com/2011/05/09/parsing-xml-in-an-iphone-app-tutorial/

   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://opentestdrive.com/Products.xml"]];

        // Perform request and get JSON as a NSData object


        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];


        NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] ;


        NSLog(@"responseString=%@", responseString);

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"GET"];

        NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
        if (conn)
        {
            receivedData = [[NSMutableData data] retain];
        }
        else
        {
        }



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}


- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"imgName"]) 
    {
        //if you Taking any NSObject Class then Alloc init here.
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementname isEqualToString:@"name"]) 
    {
        namelable.text=currentNodeContent;
    }
    if ([elementname isEqualToString:@"price"]) 
    {
        pricelabel.text = currentNodeContent;
    }
    if ([elementname isEqualToString:@"imgName"]) 
    {
        imageView.image=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:currentNodeContent]]];
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
于 2012-11-30T05:15:08.663 回答