gpt4 book ai didi

iphone - XMLParser iPhone SDK

转载 作者:行者123 更新时间:2023-11-28 18:24:08 26 4
gpt4 key购买 nike

我正在创建一个需要读取 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com