我有一个XML文件,里面有5个项目(每个都有标题和网址)我希望使用xml解析在xcode中检索所有这些项目,然后想要将所有条目存储在数组中,以便我可以在应用程序中使用它们。让我告诉你我的所有代码。
XML文件
<data>
<item>
<title>Eggs</title>
<link>
http://URL/ios/category.php?cat_id=14
</link>
</item>
<item>
<title>Bakery</title>
<link>
http://URL/ios/category.php?cat_id=15
</link>
</item>
<item>
<title>Bread</title>
<link>
http://URL/ios/category.php?cat_id=16
</link>
</item>
<item>
<title>Cakes, Pies Patisserie</title>
<link>
http://URL/ios/category.php?cat_id=17
</link>
</item>
<item>
<title>Specialty Breads</title>
<link>
http://URL/ios/category.php?cat_id=119
</link>
</item>
</data>
这是我的头文件
#import "XMLStringFile.h"
@interface MyViewController : UIViewController<NSXMLParserDelegate>{
NSMutableArray *rssOutputData;
NSMutableString *nodecontent;
NSXMLParser *xmlParserObject;
XMLStringFile *xmlStringFileObject;
}
现在在xcode中这是我的viewdidload代码
rssOutputData = [[NSMutableArray alloc]init];
NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
[xmlParserObject setDelegate:self];
[xmlParserObject parse];
这里是所有解析方法
#pragma mark NSXMLParser delegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"item"]){
xmlStringFileObject =[[XMLStringFile alloc]init];
} else {
nodecontent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(@"node content = %@",nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"item"]){
[rssOutputData addObject:xmlStringFileObject];
xmlStringFileObject = nil;
} else if([elementName isEqualToString:@"title"]){
xmlStringFileObject.xmltitle= nodecontent;
nodecontent = nil;
}
else if([elementName isEqualToString:@"link"]){
xmlStringFileObject.xmllink= nodecontent;
nodecontent = nil;
}
}
这是XMLStringFile.h和XMLStringFile.m
XMLStringFile.h
#import <Foundation/Foundation.h>
@interface XMLStringFile : NSObject {
NSString *xmllink,*xmltitle;
}
@property(nonatomic,retain)NSString *xmllink,*xmltitle;
@end
和XMLStringFile.m文件
#import "XMLStringFile.h"
@implementation XMLStringFile
@synthesize xmllink,xmltitle;
-(void)dealloc
{
}
现在我想将我的两个条目存储在两个单独的数组标题和链接中,然后我希望将来从这些数组中检索它们。我需要帮助,请让我知道我该怎么做。现在xmltitle和xmllink存储检索到的数据但是如果我在另一个函数中使用它,那么只有nslog是最后一个。
答案 0 :(得分:3)
在ViewDidLoad
方法中。
- (void)viewDidLoad {
NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://cms.proxiclients.com/choithrams/mycategories.xml"]];
NSString *XMLString = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding];
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSDictionary *dict = [XMLReader dictionaryForXMLString:XMLString error:nil];
NSLog(@"== %@",dict);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
您可以获得如下字典值。
NSString *linkText = [[[[dict valueForKey:@"data"] valueForKey:@"item"] valueForKey:@"link"] valueForKey:@"text"];
linkText = [linkText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
您的输出是字典: