有两种方式:
一:NSXMLParser
Utility.h:
#import <Foundation/Foundation.h>
@interface Utility : NSObject {
}
//获取文件在设备上的绝对路径
+ (NSString *) pathForResource:(NSString*)resourcepath;
@end
Utility.m :
#import "Utility.h"
@implementation Utility
+ (NSString*) pathForResource:(NSString*)resourcepath
{
NSBundle * mainBundle = [NSBundle mainBundle];
NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
NSString *filename = [directoryParts lastObject];
[directoryParts removeLastObject];
NSString *directoryStr = @"Xml";
NSString *path= [mainBundle pathForResource:filename
ofType:@""
inDirectory:directoryStr];
return path;
}
@end
- (void)viewDidLoad {
NSURL *xmlUrl = [NSURL fileURLWithPath:[Utility pathForResource:@"SearchLog.xml"]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
/*启动Xml解析,会触发
parser:
didStartElement:
namespaceURI:
qualifiedName:
attributes:*/
[parser parse];
//刷新Table
// [searchTableView reloadData];
NSError *parseError = [parser parserError];
if (parseError) {
}
[parser release];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
if (qName) {
elementName = qName;
}
if (xmlSearchParsedCounter >= 5) {
[parser abortParsing];
}
if ([elementName isEqualToString:@"item"]) {
xmlSearchParsedCounter++;
NSString *value = [attributeDict valueForKey:@"value"];
[self.searchList addObject:value];
}
}
二:libxml
参考:http://blog.
- 展开Targets
- 双击项目名
- 选择所有配置
- 搜索Header Search Path
- 加入一行并选中recursive选项: ${SDKROOT}/usr/include/libxml2
- 搜索Other Linker Flag
- 加入一行:-lxml2
Utility.h:
#import <Foundation/Foundation.h>
#import "TFHpple.h"
@interface Utility : NSObject {
}
+ (NSString *) pathForResource:(NSString*)resourcepath;
+ (NSMutableArray *)getXmlWithFileName:(NSString *)fileName XPath: (NSString *)xpath;
@end
Utility.m:
#import "Utility.h"
@implementation Utility
+ (NSString*) pathForResource:(NSString*)resourcepath
{
NSBundle * mainBundle = [NSBundle mainBundle];
NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
NSString *filename = [directoryParts lastObject];
[directoryParts removeLastObject];
NSString *directoryStr = @"Xml";
NSString *path= [mainBundle pathForResource:filename
ofType:@""
inDirectory:directoryStr];
return path;
}
+ (NSMutableArray *)getXmlWithFileName:(NSString *)fileName XPath: (NSString *)xpath{
NSURL *xmlUrl = [NSURL fileURLWithPath:[Utility pathForResource:fileName]];
NSData *data = [[NSData alloc] initWithContentsOfURL:xmlUrl];
TFHpple *xpathParser = [[TFHpple alloc] initWithXMLData:data];
NSArray *elements = [xpathParser search:xpath];
NSMutableArray *array = [[NSMutableArray alloc] init];
for(TFHppleElement *element in elements) {
[array addObject:[element content]];
}
[xpathParser release];
[data release];
return array;
}
@end
调用例子:
searchList = [Utility getXmlWithFileName:@"SearchLog.xml" XPath:@"//SearchLog/Item/text()"];