【问题标题】:How to parse multiple xml file in iPhone using NSxml parser如何使用 NSxml 解析器解析 iPhone 中的多个 xml 文件
【发布时间】:2012-03-26 14:56:12
【问题描述】:

如果可能的话,我在一个文件夹中有近 15 个或更多的 xml 文件来逐一解析所有文件?如果我想解析多个文件,我会在下面设置这样的路径。如何设置该文件夹文件的路径?

此代码用于单个文件 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];

所有的 XML 文件都具有相同的结构和相同的元素。

【问题讨论】:

  • 你想在一个文件中解析两个或多个xml?
  • 我猜你需要为每个进程创建线程,这样你的所有解析进程都会在后台同时运行。
  • @PiyushPatel 不,我想解析多个我想将该文件设置为路径。这就是我需要的。
  • @mrunal 好的,但我如何设置所有文件的路径?

标签: iphone xml-parsing xcode4.2


【解决方案1】:

@moorthy 在 NSBundle 方法上使用 pathForResourcesType:inDirectory 获取路径数组,然后为数组中的每个路径创建解析器类的实例并解析文件。 [[NSBundle mainbundle]pathForResourcesType:@"xml" inDirectory:"Your directory Path"]
这将返回一个数组......希望它可以帮助你

【讨论】:

    【解决方案2】:

    试试这个:

    NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithData:playlistfileData]autorelease];
    
    PlaylistXmlParser *parser = [[PlaylistXmlParser alloc] initXMLParser:@"xmlname"];
    [xmlParser setDelegate:parser];
    

    在 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 
    

    }

    【讨论】:

    • initXMLParser:@"xmlname"];我想在这里给文件夹文件,我的意思是 n 个文件我怎么给。
    • 只需识别要解析的 xml,以便您可以在解析中分开
    • 你也可以这样使用:initXMLParser:[NSString stringWithFormat:@"%d",i]];
    • @"xmlname" 什么是单个文件? agin 我说过我想给这里文件夹文件。
    • NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *xml_path = [documentsDir stringByAppendingPathComponent:@"xmlfolder"]; NSArray *a =[NSBundle pathsForResourcesOfType:@"xml" inDirectory:xml_path]; NSLog(@"%@",a);
    【解决方案3】:

    在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.
    }
    

    和上面一样,NSXMLParser 的所有代表都有一个参数作为 NSXMLParser 对象。

    【讨论】:

    • 感谢您的回复您是否理解我的问题。这不是这个问题的答案。我已经提到我可以解析一个我想解析多个文件的文件。
    • 创建多个对象并在使用委托检查哪个对象即将到来之后为它们提供适当的文件数据,然后编写业务逻辑来解析数据。
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2012-07-14
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多