1

Mac アプリケーションを作成していますが、ローカルの xml ファイルを解析して tableView に表示する必要があります。何らかの理由で、xml にある文字が見つかったため、tableView に空白の行が表示されますが、これは意味がありません。これが私のコードです:

- (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>
4

2 に答える 2

1

エラーはパーサーにあります。ロジックを修正してください。itemテーブルビュー配列を埋めるときにオブジェクトを使用していません。また、XML 要素間のテキストをキャッチしておらず、適切な変数に割り当てていません。

次の点に注意してください。

  • 要素に入るときは、現在どの要素にいるのかを追跡します
  • 文字を見つけたら、現在の要素に応じて適切な属性変数を入力する必要があります
  • 要素を終了したら、すべての入力されたキーをデータ配列にevent追加する必要があります。item
于 2013-02-03T19:11:55.783 に答える
0

配列の使い方を知る必要があります。配列ストーリーを 1 つ割り当てています。しかし、それから使用していません。didEndElement メソッドを確認してください。

Event の 1 つのクラスを作成し、.h および .m ファイルを作成してから、すべての要素のプロパティを作成し、Eventクラスのオブジェクト全体を配列に追加します。その配列は、appHandler または Single ton クラスで使用できます。

このことを確認してください。お役に立てますように。

于 2013-02-03T20:50:11.137 に答える