简体   繁体   English

如何使用NSxml解析器解析iPhone中的多个xml文件

[英]How to parse multiple xml file in iPhone using NSxml parser

I have nearly 15 and more xml file inside of one folder if possible to parse all file one by one? 如果可以一一解析所有文件,我在一个文件夹中有将近15个和更多xml文件? I set path like this below if I want to parse multiple files. 如果要解析多个文件,则在下面设置以下路径。 how can I set path of that folder file? 如何设置该文件夹文件的路径?

this code for single file xml parser it's working fine. 单个文件xml解析器的这段代码可以正常工作。

NSString *playlistfilePath = [[NSBundle mainBundle] pathForResource:@"CT8OkzhF8qmEYGe2" ofType:@"xml"];  
NSData *playlistfileData = [NSData dataWithContentsOfFile:playlistfilePath]; 
NSString *playlistxmlFile = [[NSString alloc] initWithData:playlistfileData encoding:NSASCIIStringEncoding];

//parsing the XML
PlaylistXmlParser *playlistparser = [[PlaylistXmlParser alloc] init];
[playlistparser parseXMLFile:playlistxmlFile];

that all the XML files are having same structure and same element. 所有XML文件都具有相同的结构和相同的元素。

On the basis of the NSXMLParser object write the logic of the parsing:- 在NSXMLParser对象的基础上,编写解析逻辑:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
        //check with switch or if else condition which NSXMLParser object is using this delegate.
}

As like above all the delegates of NSXMLParser have a parameter as an NSXMLParser object. 像上面一样,NSXMLParser的所有委托都有一个参数作为NSXMLParser对象。

try this: 尝试这个:

NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithData:playlistfileData]autorelease];

PlaylistXmlParser *parser = [[PlaylistXmlParser alloc] initXMLParser:@"xmlname"];
[xmlParser setDelegate:parser];

in PlaylistXmlParser.m file 在PlaylistXmlParser.m文件中

- (PlaylistXmlParser *) initXMLParser:(NSString *)name {

[super init];
xmlname =name;

return self;

} }

now in every methods in PlaylistXmlParser.m file  :

if  ([xmlname isEqualToString:@"xmlname"]) {
    //store  data 
}
else if([xmlname isEqualToString:@"xmlname_1"]){
   //store  data 

} }

@moorthy use pathForResourcesType:inDirectory on NSBundle method to get array of paths, Then for every single path inside the array create an instance for the parser class and parse the file. @moorthy在NSBundle方法上使用pathForResourcesType:inDirectory获取路径数组,然后为数组中的每个单个路径创建解析器类的实例并解析文件。 [[NSBundle mainbundle]pathForResourcesType:@"xml" inDirectory:"Your directory Path"]
This will return an array ...... Hope it helps you 这将返回一个数组……希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM