从xml内容字符串访问关键字

时间:2014-08-28 10:14:41

标签: ios objective-c iphone nsstring

我有一个包含一些文字的网址:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>title</key><string>HELLO </string><key>image</key><string>http://appersian.org/upcenter/images/main3-1409057358.jpg</string><key>description</key><string>YOOHOOO</string></dict></plist>

我需要访问<key>title</key>等关键字并将其显示为其他字符串,这是我加载网址的代码:

 NSURL *URL = [NSURL URLWithString:@"http://appersian.net/sondos/services?action=events&num=1"];
    NSError *error ;
    NSString *PageContent = [NSString stringWithContentsOfURL:URL
                                                      encoding:NSASCIIStringEncoding
                                                         error:&error];
   NSLog(@"%@",PageContent);

已编辑:

 NSURL *URL = [NSURL URLWithString:@"http://appersian.net/sondos/services?action=events&num=1"];
    NSData *data = [NSData dataWithContentsOfURL:URL];
    NSError *error = nil;
    NSDictionary *dict = [XMLReader dictionaryForXMLData:data
                                                 options:XMLReaderOptionsProcessNamespaces
                                                   error:&error];


    NSString *string = [dict objectForKey:@"title"];
    NSLog(@"%@",string);

但是返回null!

1 个答案:

答案 0 :(得分:1)

使用NSDictionary提供XMLString XMLDataNSXMLParser使用NSError *error = nil; NSDictionary *dict = [XMLReader dictionaryForXMLString:xmlStringHere options:XMLReaderOptionsProcessNamespaces error:&error]; NSLog(@"%@",dict);

Uasage

gtm_stringByUnescapingFromHTML

解决方案:

在项目中添加XMLReader以使用方法NSURL *URL = [NSURL URLWithString:@"http://appersian.net/sondos/services?action=events&num=1"]; NSError *error1; NSString *strPageContent = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error1]; strPageContent = [strPageContent gtm_stringByUnescapingFromHTML]; if ([strPageContent rangeOfString:@"<plist version=\"1.0\">"].location != NSNotFound) { NSRange range = [strPageContent rangeOfString:@"<plist version=\"1.0\">"]; strPageContent = [strPageContent substringWithRange:NSMakeRange(range.location+range.length, strPageContent.length-(range.location+range.length))]; strPageContent = [strPageContent stringByReplacingOccurrencesOfString:@"</plist>" withString:@""]; } NSLog(@"%@",strPageContent); NSError *error = nil; NSDictionary *dictData = [XMLReader dictionaryForXMLString:strPageContent options:XMLReaderOptionsProcessNamespaces error:&error]; NSLog(@"dict: %@",dictData);

if ([dictData count]>0) {
    NSDictionary *dictInner = [dictData objectForKey:@"dict"];
    //NSArray *arrkeys = [dictInner objectForKey:@"key"];
    NSArray *arrValues = [dictInner objectForKey:@"string"];

    NSString *strTitle = [[arrValues objectAtIndex:0] objectForKey:@"text"];
    NSString *strImage = [[arrValues objectAtIndex:1] objectForKey:@"text"];
    NSString *strDescription = [[arrValues objectAtIndex:2] objectForKey:@"text"];

    NSLog(@"Title: %@ | Image: %@ | Desc: %@",strTitle,strImage,strDescription);
}

编辑:获取数据

{{1}}