XML 到 Xcode 正在解析

XML to Xcode Parsing

我正在将 XML 文件中的数据解析到我在 Xcode 中的应用程序。它正在解析,但 UITableView 中未显示第一个值,请帮忙。让我向您展示我的代码。

ViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSLog(@"node content = %@",nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle=nodecontent;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink=nodecontent;
    }
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    }
    nodecontent=[[NSMutableString alloc]init];
}

还有我的 XML 文件

<?xml version="1.0"?>
<data>
      <item><title>Eggs</title>
      <link>http://url/category.php?cat_id=15</link></item>
      <item><title>Bakery</title>
      <link>http://url/category.php?cat_id=15</link></item>
      <item><title>Bread</title>
      <link>http://url/category.php?cat_id=16</link></item>
      <item><title>Cakes, Pies Patisserie</title>
      <link>http://url/category.php?cat_id=17</link></item>
      <item><title>Specialty Breads</title>
      <link>http://url/category.php?cat_id=119</link></item>
</data>

现在在输出中我可以看到所有条目但看不到第一个条目的标题"Eggs"

是的,这是因为你在 NSXMLParser 委托方法 didEndElement: 中初始化 nodecontent 字符串。

那么,当解析器找到第一个对象时会发生什么,它将初始化 nodecontent 字符串,并且第一个对象不会附加到字符串,因为它不是那个时候初始化的,而是第二个对象,所有剩余的都将添加到 nodecontent 因为它是在第一次调用后初始化的。

因此,在解析 XML 文件之前,最好在 viewDidLoad() 方法中分配初始化 nodecontent

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 方法中删除行 nodecontent=[[NSMutableString alloc]init];

- (void)viewDidLoad {
    [super viewDidLoad];

    nodecontent=[[NSMutableString alloc]init];

    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle=nodecontent;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink=nodecontent;
    }
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    }
}

首先,你不应该在 didEndElement 方法中分配你的 nodecontent :当你的解析结束时,你将有一个分配的对象。

试试这个方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    } else {
     nodecontent = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    } else if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle= nodecontent;
        nodecontent = nil;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink= nodecontent;
        nodecontent = nil;
    }
}