XML在Xcode解析问题?(XML Parsing issue in Xcode?)

2019-08-16 17:14发布

我想提出一个MAC的应用程序,我需要它来解析本地XML文件,并将其显示成的tableView。 出于某种原因,我得到了我的tableView,这是没有意义的,因为它已经发现,在我的XML字符一个空白行。 这里是我的代码:

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"found file and started parsing");
}

- (void)parseXMLFileAtURL:(NSString *)URL
{
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];


    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate: self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
    [rssParser parse];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSString * errorString = [NSString stringWithFormat:@"Unable to download XML feed (Error code %i )", [parseError code]];
    NSLog(@"Error parsing XML: %@", errorString);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    //NSLog(@"found this element: %@", elementName);
    currentElement = [elementName copy];

    if ([elementName isEqualToString:@"event"]) {
        // clear out our story item caches...
        item = [[NSMutableDictionary alloc] init];
        date = [[NSMutableString alloc] init];
        opponent = [[NSMutableString alloc] init];
        location = [[NSMutableString alloc] init];
        time = [[NSMutableString alloc] init];
        games = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"ended element: %@", elementName);

    if ([elementName isEqualToString:@"event"]) {
        // save values to an item, then store that item into the array...
        [item setObject:date forKey:@"date"];
        [item setObject:opponent forKey:@"opponent"];
        [item setObject:location forKey:@"location"];
        [item setObject:time forKey:@"time"];
        [item setObject:games forKey:@"games"];
        NSMutableArray *stories = [[NSMutableArray alloc] init];
        [stories addObject:[item copy]];

        [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                    date, @"date", 
                                    opponent, @"opponent",
                                    location, @"location",
                                    time, @"time",
                                    games, @"games"
                                    , nil]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...

    if ([date isEqualToString:@"date"]) {
        [date appendString:string];
    } else if ([opponent isEqualToString:@"opponent"]) {
        [opponent appendString:string];
    } else if ([location isEqualToString:@"location"]) {
        [location appendString:string];
    } else if ([time isEqualToString:@"time"]) {
        [time appendString:string];
    } else if ([games isEqualToString:@"games"]) {
        [games appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"all done!");
    [tabelview reloadData];
}

当我删除我的增加部分,在那里它添加项目到arraycontroller,并添加

[arrayController addObject:stories]; 我得到的布赫('s

如果你还需要什么,不只是反对票,而是告诉我。 谢谢!

这里是我的xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<xmlData>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
</xmlData>

Answer 1:

该错误是在你的解析器。 请修改逻辑。 你不使用你的对象item填写你的表视图阵列时。 另外,你不要再追在XML元素之间的文本,而不是将它们分配给相应的变量。

请注意以下几点:

  • 当你进入一个元素,跟踪哪些元素你是在目前
  • 当你发现字符你必须填写适当的属性变量根据您是在哪个元素
  • 当您完成event元素,你应该添加您的item及其所有填写按键到你的数据阵列。


Answer 2:

你需要知道如何使用数组。 您分配一个阵列的故事 。 但随后没有使用。 请检查您的didEndElement方法。

使一类的事件,创造h和.m文件,然后创建你的所有元素的属性,然后添加事件类的整个对象到一个数组。 这阵你可以在appHandler或单吨级。

检查这个东西。 它可以帮助你。



文章来源: XML Parsing issue in Xcode?