-1

我知道如何做到这一点,但我的元素名称是相同的 prakesh 并且所有属性名称也相同所以如何获取 prakesh 元素属性值tableview请解释我:

<?xml version="1.0" encoding="UTF-8"?>
    <prakesh ConfigVersion="1">
        <prakesh ProductId="{ec8e531b-6873-4819-8ade-501f6dc6ff42}" ProductVersion="1.0" ProductLogo="" ProductName="4x6_Glossy" ProductRefImage="4x6_Glossy.png" CustomerType="1" Dpi="300" Unit="0" CoverType="1" CoverWidth="16.5" CoverHeight="12" CoverTopMargin="0.3" CoverBottomMargin="0.3" CoverLeftMargin="0.3" CoverRightMargin="0.3" PageWidth="4" PageHeight="6" PageTopMargin="0.3" PageBottomMargin="0.3" PageLeftMargin="0.3" PageRightMargin="0.3" Comments="4 x 6 Inches" BasePrice="10" PricePerPage="0" BasePriceWithDesign="0" PricePerPageWithDesign="0" FixQuantity="0" Finish="Glossy" ServiceName="Photoprints"/>
        <prakesh ProductId="{6e5b2453-f0e2-43c8-a7d6-1900346b9f83}" ProductVersion="1.0" ProductLogo="" ProductName="4x6_Matt" ProductRefImage="4x6_Matt.png" CustomerType="1" Dpi="300" Unit="0" CoverType="1" CoverWidth="16.5" CoverHeight="12" CoverTopMargin="0.3" CoverBottomMargin="0.3" CoverLeftMargin="0.3" CoverRightMargin="0.3" PageWidth="4" PageHeight="6" PageTopMargin="0.3" PageBottomMargin="0.3" PageLeftMargin="0.3" PageRightMargin="0.3" Comments="4 x 6 Inches" BasePrice="10" PricePerPage="0" BasePriceWithDesign="0" PricePerPageWithDesign="0" FixQuantity="0" Finish="Matt" ServiceName="Photoprints"/>
</prakesh>

并做到了这一点,但我只得到了第一个 prakesh productid 和所有。如何获得第二个?这是我的代码:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"Name:%@",elementName);
    _forecastInfo.ProductId = [attributeDict objectForKey:@"ProductId"];
    _forecastInfo.ProductName = [attributeDict objectForKey:@"ProductName"];
    _forecastInfo.ProductRefImage=[attributeDict objectForKey:@"ProductRefImage"];
    _forecastInfo.CustomerType=[attributeDict objectForKey:@"CustomerType"];
    _forecastInfo.Dpi=[attributeDict objectForKey:@"Dpi"];
    _forecastInfo.Unit=[attributeDict objectForKey:@"Unit"];
    _forecastInfo.CoverType=[attributeDict objectForKey:@"CoverType"];
    _forecastInfo.CoverWidth=[attributeDict objectForKey:@"CoverWidth"];
     _forecastInfo.CoverHeight=[attributeDict objectForKey:@"CoverHeight"];



    if(_forecastInfo.ProductId||_forecastInfo.ProductName||_forecastInfo.ProductRefImage||_forecastInfo.CustomerType||_forecastInfo.Dpi|| _forecastInfo.Unit||_forecastInfo.CoverType||_forecastInfo.CoverWidth||_forecastInfo.CoverHeight)
    {
        //[productIdArray addObject:productId];
        [parser abortParsing];
        NSLog(@"%@",[_forecastInfo description]);
        if(delegate)
            [delegate forecastInfoParser:self parsed:_forecastInfo];
        [_forecastInfo release];
    }

}
4

1 回答 1

0

请执行下列操作:

  • 创建一个globle NSMutableArray(让我们命名它dataArray
  • 现在像这样启动它

    dataArray = [[NSMutableArray alloc] init]; //(如果您使用的是非 ARC 环境,请释放它。)

  • 现在在你的方法中:

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *) attributeDict
     {
         NSLog(@"Name:%@",elementName);
         _forecastInfo.ProductId = [attributeDict objectForKey:@"ProductId"];
         _forecastInfo.ProductName = [attributeDict objectForKey:@"ProductName"];
         _forecastInfo.ProductRefImage=[attributeDict objectForKey:@"ProductRefImage"];
         _forecastInfo.CustomerType=[attributeDict objectForKey:@"CustomerType"];
         _forecastInfo.Dpi=[attributeDict objectForKey:@"Dpi"];
         _forecastInfo.Unit=[attributeDict objectForKey:@"Unit"];
         _forecastInfo.CoverType=[attributeDict objectForKey:@"CoverType"];
         _forecastInfo.CoverWidth=[attributeDict objectForKey:@"CoverWidth"];
         _forecastInfo.CoverHeight=[attributeDict objectForKey:@"CoverHeight"];
         if(_forecastInfo.ProductId||_forecastInfo.ProductName||_forecastInfo.ProductRefImage||_forecastInfo.CustomerType||_forecastInfo.Dpi|| _forecastInfo.Unit||_forecastInfo.CoverType||_forecastInfo.CoverWidth||_forecastInfo.CoverHeight)
         {
             //[productIdArray addObject:productId];
             //[parser abortParsing];
            [dataArray addObject:[_forecastInfo copy]];
             NSLog(@"%@",[_forecastInfo description]);
            if(delegate)
                [delegate forecastInfoParser:self parsed:_forecastInfo];
            [_forecastInfo release];
         }
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
        if ([elementName isEqual:@"prakesh"]){
            [parser abortParsing];
        }
    }
    
  • tableView现在在使用中填充数据dataArray

于 2013-02-25T09:04:29.057 回答