我有一个包含5个项目(每个都有标题和URL)的XML文件,我想使用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最后的整个过程。
最佳答案
下载XMLReader
在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]];
您的输出是字典: