gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:38:49 25 4
gpt4 key购买 nike

我有一个包含一些文本的 URL:

<?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> 这样的关键字并将它们显示到另一个字符串中,这是我加载 URL 的代码:

 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 !

最佳答案

使用XMLReader使用 NSXMLParser

提供 XMLStringXMLDataNSDictionary

用法:

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

解决方案:

添加GTMNSString-HTML在您的项目中使用方法 gtm_stringByUnescapingFromHTML

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);
}

关于ios - 从 xml 内容字符串访问关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25546276/

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