iphone - XMLParser iPhone SDK

标签 iphone objective-c ios nsxmlparser

我正在创建一个需要读取 XML file 的 iPhone 应用程序

属性

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *fileName;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSString *link;

搜索标签(这里只搜索“title”)

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    // FIRST TAG XML
    if([elementName isEqualToString:@"title"]){
        // GET DATA FROM XML 
        getData = YES; // getData is a bool which is NO initialy
        NSLog(@"Found");
    }
    else
        NSLog(@"Not Found");

}

获取数据

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if(getData)
    {
        // CREATION DE L OBJET DEPUIS LE XML
        NSLog(@"This is your desired data = %@",string);
        NSLog(@"Object creation ...");
        Pebkac *peb = [[Pebkac alloc] init];
        NSLog(@"Setup the name ...");
        [peb setName:@"Pebkac"];
        NSLog(@"Small descrption ...");
        [peb setDescription:string];
        [pebkacs addObject:peb];
    }
}

结束标签

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    // XML END TAG
    if ([elementName isEqualToString:@"title"]){
        getData = NO;
    }
}

我观察到的错误是,有时它无法获取标签之间的所有文本。

最佳答案

foundChars 可以被调用N次。您总是将字符串设置为到达那里的字符。您必须一次又一次地附加字符,当您收到 endTag 调用时,您必须设置结果。

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if(!foundChars)
        foundChars = [[NSMutableString alloc] init];
    [foundChars appendString:string];
}

然后在最后使用它 -- 编写的示例适合您的问题

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if(getData)
    {
        // CREATION DE L OBJET DEPUIS LE XML
        NSLog(@"This is your desired data = %@",string);
        NSLog(@"Object creation ...");
        Pebkac *peb = [[Pebkac alloc] init];
        NSLog(@"Setup the name ...");
        [peb setName:@"Pebkac"];
        NSLog(@"Small descrption ...");
        [peb setDescription:foundChars];
        [pebkacs addObject:peb];
    }

    //reset
    foundChars = nil;

    // XML END TAG
    if ([elementName isEqualToString:@"title"]){
        getData = NO;
    }
}

关于iphone - XMLParser iPhone SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044424/

相关文章:

ios - 如何在 ARkIt 2 中将图像包裹在 3d 对象上?

objective-c - 更新后,应用程序不会从文档文件夹加载现有文件

objective-c - Swift 中相当于 Controller 的自类分配

ios - Swift SceneKit 照明和影响发射纹理

iphone - 如何将数值附加到数组

iphone - 使用 EKEventStore 从 iPhone 日历中删除事件

objective-c - Objective-C中的类构成问题: Is it possible to inherit a class variable?

ios - 当我使用 ReactiveKit/Bond 调用 becomeFirstResponder 后,UITextField 立即退出第一响应者

ios - AVPlayerViewController - 无自动布局 - 无法同时满足约束条件。

iphone - 如何在没有 UIimagePickerController 的情况下以编程方式在 iphone 中访问保存图像中的图像?