0

これは私のcountries.xmlファイルです。

問題は何ですか pls は私に答えてください私は iOS が初めてです.....

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
<countris>
<country>
    <countryname>india<countryname>    
</country>
</countris>
</plist>

これは私のviewDidLoadです:

NSString *filename = [[NSBundle mainBundle] pathForResource:@"countris" ofType:@"xml"];
NSData *data = [NSData dataWithContentsOfFile:filename];


NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];

Myparser *myParser = [[Myparser alloc] init];

[parser setDelegate:myParser];

BOOL success = [parser parse];

if (success)
{
    NSMutableArray *users = [myParser arraydata];
    NSString *log= [NSString stringWithFormat:
                          @"This is an array of User objects:\n\n%@",
                          [users description]];
    NSLog(log);
}
else
{
    NSLog(@"Error parsing document!");

    [[[UIAlertView alloc] initWithTitle:nil
                                message:@"Error parsing document!"
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}

NSXMLParserDelegate プロトコルを実装する Myparser.m ファイル

#import "Myparser.h"
#import "MyParserData.h"

@implementation Myparser

@synthesize mydata,myparser,arraydata,flag,currentElementValue;

- (id) init {

    if(self = [super init]) {
        currentElementValue = [[NSMutableString alloc] initWithString:@""];
        arraydata = [[NSMutableArray alloc] init];
        flag = FALSE;
    }
    return self;
}

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

    if ([elementName isEqualToString:@"countris"]) {
        return;
    } else if ([elementName isEqualToString:@"country"]) {
        self.mydata  = [[MyParserData alloc] init];
         } else {
        self.currentElementValue = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (self.currentElementValue) {
        [self.currentElementValue appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parserdidEndElement:(NSString *)elementNamenamespaceURI:          (NSString *)namespaceURIqualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"counteis"]) {

        return;
    }

    if ([elementName isEqualToString:@"country"]) {
        [self.arraydata addObject:mydata];
        self.mydata = nil;
    } else {
        [self.mydata setValue:self.currentElementValue forKey:elementName];
    }

    self.currentElementValue = nil;
}

@end
4

3 に答える 3

1

XML が無効です。タグがありますが、<countryname>決して閉じません (つまり、/inがありません</countryname>)。

を使用する場合は、 parseErrorOccurredNSXMLParserを実装してからエラー オブジェクトを確認することをお勧めします。これにより、多くの場合、XML ソースで問題を探す場所についての洞察が得られます。Cocoa フレームワークは一般に、エラーに関する情報を取得するためのメソッドを提供するのに優れているため、これらの手法を利用してください。


ところで、フォーマットを使用しようとしているように見えますがplist、手動で XML を記述しているかのように、適切なフォーマットに完全には準拠していません。通常、plistファイルを使用している場合は、を使用する必要はまったくありませんNSXMLParser(また、XML ソースを手動で作成する必要もありません)。

次のソース コードを想像してください。

NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filename = [docsPath stringByAppendingPathComponent:@"test.plist"];

NSDictionary *dictionary = @{@"countries": @{@"country" : @"india"}};
[dictionary writeToFile:filename atomically:YES];

これにより、test.plist次のようなファイルが作成されます。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>countries</key>
    <dict>
        <key>country</key>
        <string>india</string>
    </dict>
</dict>
</plist>

問題は、 を使用して plist を作成すると、次のwriteToFileように読み取ることができるようになることです。

dictionary = [NSDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"dictionary = %@", dictionary);

明らかに、 の使用方法を学習しようとしている場合NSXMLParserは、実験を続けてください。ただし、ファイルを使用する場合は、保存と読み込みplistを使用するだけでプロセスを簡素化できます。writeToFiledictionaryWithContentsOfFile

ちなみに、このプロセスはNSDictionaryオブジェクトだけでなく、NSArrayオブジェクトでも機能します。たとえば、次の辞書の配列を考えてみましょう。

// create array plist

filename = [docsPath stringByAppendingPathComponent:@"array.plist"];
NSArray *array = @[@{@"country" : @"india"}, @{@"country" : @"france"}];
[array writeToFile:filename atomically:YES];

// read array plist

array = [NSArray arrayWithContentsOfFile:filename];

ただし、辞書と配列を XML 形式で保存でき、XML ソースに手を加えたり、NSXMLParser.

詳細については、「プロパティ リスト プログラミング ガイド 」を参照してください。


また、配列と辞書にカスタム オブジェクト、つまり標準の Cocoa オブジェクト以外のNSオブジェクト ( NSStringNSNumber、など) が含まれている場合、とというNSDate別のテクノロジがあり、バイナリファイルを記述できます。これも XML 形式を使用しますが、 XML ソースを自分で書くことや、. これはもう少し複雑で、カスタム クラスを使用して配列や辞書を扱っている場合を除き、気にする必要はありません。NSKeyedArchiverNSKeyedUnarchiverplistNSXMLParser

于 2013-05-27T13:19:02.680 に答える