1

从远程 XML 文件中解析元素后,我在NSArray. 由于数组包含更多这样的元素和值,

success; products=<AGSProducts 0x7189020; name='category1';imageIdentifier='1'; imageUrlString='http://myimage.com/images/img1.png'; subProducts=>

我需要imageUrlString从这个 NSArarray 中获取。如何从这些数组中单独获取这个特定的 imageUrlString。

我的视图控制器

@property(nonatomic,strong) NSMutableArray *xmlProductsResults;

[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://myxml.com/Category.xml"];
EGSParser *parser = [[EGSParser alloc] init];
if ([parser loadXMLByURL:url]){
  self.xmlProductsResults = parser.products;
  NSArray *myArray = [_xmlProductsResults objectAtIndex:0];
  NSLog(@"success; products=%@", myArray);
}

请给我一个想法

4

4 回答 4

2

希望这可以帮助。

编辑 2

NSString *string = [myArray description];
NSString *url = nil;
NSScanner *parser = [NSScanner scannerWithString:string];
[parser scanUpToString:@"imageUrlString" intoString:nil];

if (![parser isAtEnd]) {
    [parser scanUpToString:@"=" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
    [parser scanUpToCharactersFromSet:charset intoString:nil];
    [parser scanCharactersFromSet:charset intoString:nil];
    [parser scanUpToCharactersFromSet:charset intoString:&url];

}

NSLog(@"URL: %@", url);
于 2013-02-21T09:27:27.800 回答
1

在字典中转换您的回复

NSMutableDictionary *dic = [myString JSONValue];

从 dic 你可以得到imageUrlString

于 2013-02-21T09:11:56.003 回答
1

警告:这不是最佳实践,但可以解决问题并且效率很高......

NSString *result = @"";
NSRange r1 = [myString rangeOfString:@"imageUrlString='"];
if(r1.location != NSNotFound)
{
    result = [myString substringFromIndex:r1.location+r1.length];
    NSRange r2 = [result rangeOfString:@"'"];
    if(r2.location != NSNotFound)
    {
        result = [result substringToIndex:r2.location];
    }
    else
    {
        result = @"";
    }
}

NSLog(@"%@", result);
于 2013-02-21T09:19:13.090 回答
1

试试这个。

    //NSArray *arr = [NSArray arrayWithObjects:@"<AGSProducts 0x7189020; name='category1';imageIdentifier='1'; imageUrlString='http://myimage.com/images/img1.png'; subProducts=>", nil];
    //NSString *imgURL= [arr objectAtIndex:0];

    NSString *imgURL=[_xmlProductsResults objectAtIndex:0];

    imgURL = [imgURL substringFromIndex:[imgURL rangeOfString:@"imageUrlString="].location+[@"imageUrlString=" length]+1];
    imgURL = [imgURL substringToIndex:[imgURL rangeOfString:@";"].location-1];
    NSLog(@"%@",imgURL);
于 2013-02-21T09:37:22.493 回答