Ho cercato per circa due ore ora cercando di scoprire che cos'è questo errore e come risolverlo. documenti di Apple dice volentieriAiuto per risolvere l'errore NSXMLParser 39: NSXMLParserAttributeNotStartedError
NSXMLParserAttributeNotStartedError attributo non è stato avviato. Disponibile in Mac OS X v10.3 e versioni successive. Dichiarato in NSXMLParser.h.
ma non fornisce alcun tipo di aiuto. Qualcuno può aiutarmi qui? Ogni spinta nella giusta direzione è molto apprezzata.
In viewDidLoad, ho:
NSURL *xmlUrl = [NSURL fileURLWithPath:xmlFilepath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:NO];
BOOL success = [xmlParser parse];
Nessun problema con ottenere il codice XML, posso leggere con successo fino a quando l'elemento principale, poi si rompe.
Il resto del relativo codice va come:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"foo"]) {
foo = [[NSMutableArray alloc] init];
} else if([elementName isEqualToString:@"bar"]) {
bar = [[Bar alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundAttributeDeclarationWithName:(NSString *)attributeName forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue {
if ([elementName isEqualToString:@"bar"]) {
if ([attributeName isEqualToString:@"barAttrib1"]) {
[bar setBarAttrib1:[defaultValue intValue]];
} else if ([attributeName isEqualToString:@"barAttrib2"]) {
[bar setBarAttrib2:defaultValue];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// nothing inside the elements, just attributes
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"bar"]) {
[foo addObject:bar];
[bar release];
bar = nil;
}
}
Grazie in anticipo.
EDIT: esempio XML
<foo>
<bar barAttrib1=1 barAttrib2="hello" />
<bar barAttrib1=2 barAttrib2="world" />
</foo>
Si prega di inviare l'esempio XML troppo – Yuji
Yuji, vedere aggiornato posta. Grazie! – Altealice
+1 per aver fatto una bella domanda. – Sid