Hallo an alle : ),
ich bin gerade am durchdrehen, versuche mein XML zu Parsen.
Dies funktioniert auch beim einem einfachen aufbau wie diesen:
Alles anzeigen
jetzt versuche ich aber auch die Artikel und die Unterkategorien auszulesen:
Alles anzeigen
hier der code:
Alles anzeigen
Konsolen auszug:
Alles anzeigen
Wie zu sehen ist hat er aber anscheinend den wert aus der XML ausgelesen?
Hat jemand eine idee?
ich bin gerade am durchdrehen, versuche mein XML zu Parsen.
Dies funktioniert auch beim einem einfachen aufbau wie diesen:
XML-Quellcode
jetzt versuche ich aber auch die Artikel und die Unterkategorien auszulesen:
XML-Quellcode
- <?xml version="1.0" encoding="UTF-8" ?>
- <usr_web2_7>
- <kategorien>
- <kid>1</kid>
- <kategorie>Arbeit und Wirtschaft</kategorie>
- <pikto>Arbeit_Wirtschaft.png</pikto>
- <ukat>
- <sub>Ausbildung</sub>
- <con>
- <eintrag>
- <url>http://www.will keine werbung machen.de/1.html/</url>
- <title>titel 1</title>
- <des>text 1 ...</des>
- </eintrag>
- <eintrag>
- <url>http://www.will keine werbung machen.de/2.html/</url>
- <title>titel2</title>
- <des>text 2 ...</des>
- </eintrag>
- </con>
- </ukat>
- <ukat>
- <sub>Börse</sub>
- <con>
- <eintrag>
- <url>http://www.will keine werbung machen.de/3.html/</url>
- <title>titel 3</title>
- <des>text 3 ...</des>
- </eintrag>
- </con>
- </ukat>
- </kategorien>
- </usr_web2_7>
hier der code:
Quellcode
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
- if ([elementName isEqualToString:@"usr_web2_7"]){
- //Initia. array.
- appDelegate.books = [[NSMutableArray alloc] init];
- }
- else if ([elementName isEqualToString:@"kategorien"]){
- //Initia. book.
- aBook = [[Book alloc] init];
- aBook.kid = [[NSMutableArray alloc] init];
- }
- else if ([elementName isEqualToString:@"ukat"]){
- aBook = [[Book alloc] init];
- aBook.kid = [[NSMutableArray alloc] init];
- }
- else if ([elementName isEqualToString:@"con"]){
- aBook = [[Book alloc] init];
- aBook.kid = [[NSMutableArray alloc] init];
- }
- else if ([elementName isEqualToString:@"eintrag"]){
- aBook = [[Book alloc] init];
- aBook.kid = [[NSMutableArray alloc] init];
- }
- NSLog(@"Element: %@", elementName);
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- if(!currentElementValue)
- currentElementValue = [[NSMutableString alloc] initWithString:string];
- else
- [currentElementValue appendString:string];
- NSLog(@"Value: %@", currentElementValue);
- }
- - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
- namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
- // ignoriere Root und leere Elemente
- if (( [elementName isEqualToString:@"usr_web2_7"]) || ( [elementName isEqualToString:@"ukat"] ) || ( [elementName isEqualToString:@"con"] ) || ( [elementName isEqualToString:@"eintrag"] ) )
- return;
- if([elementName isEqualToString:@"kategorien"]) {
- [appDelegate.books addObject:aBook];
- [aBook release];
- aBook = nil;
- }
- if([elementName isEqualToString:@"ukat"]) {
- [appDelegate.books addObject:aBook];
- [aBook release];
- aBook = nil;
- }
- if([elementName isEqualToString:@"con"]) {
- [appDelegate.books addObject:aBook];
- [aBook release];
- aBook = nil;
- }
- if([elementName isEqualToString:@"eintrag"]) {
- [appDelegate.books addObject:aBook];
- [aBook release];
- aBook = nil;
- }
- else
- [aBook setValue:currentElementValue forKey:elementName];
- [currentElementValue release];
- currentElementValue = nil;
- }
Konsolen auszug:
Quellcode
- 2010-10-30 15:24:40.665 XML[2559:207] Element: sub
- 2010-10-30 15:24:40.666 XML[2559:207] Value:
- Ausbildung
- 2010-10-30 15:24:40.668 XML[2559:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Book 0x6b04470> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key sub.'
- *** Call stack at first throw:
- (
- 0 CoreFoundation 0x02488b99 __exceptionPreprocess + 185
- 1 libobjc.A.dylib 0x025d840e objc_exception_throw + 47
- 2 CoreFoundation 0x02488ad1 -[NSException raise] + 17
- 3 Foundation 0x000320f3 _NSSetUsingKeyValueSetter + 135
- 4 Foundation 0x00032061 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
- 5 XML 0x00003719 -[XMLParser parser:didEndElement:namespaceURI:qualifiedName:] + 1008
- 6 Foundation 0x000f33a9 _endElementNs + 453
- 7 libxml2.2.dylib 0x02915ea7 xmlParseXMLDecl + 1346
- 8 libxml2.2.dylib 0x02920bb1 xmlParseChunk + 3984
- 9 Foundation 0x000f2baa -[NSXMLParser parse] + 321
- 10 XML 0x000021e3 -[XMLAppDelegate applicationDidFinishLaunching:] + 304
- 11 UIKit 0x002baf80 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252
- 12 UIKit 0x002bd3b0 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
- 13 UIKit 0x002c73ec -[UIApplication handleEvent:withNewEvent:] + 1958
- 14 UIKit 0x002bfb3c -[UIApplication sendEvent:] + 71
- 15 UIKit 0x002c49bf _UIApplicationHandleEvent + 7672
- 16 GraphicsServices 0x02d68822 PurpleEventCallback + 1550
- 17 CoreFoundation 0x02469ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
- 18 CoreFoundation 0x023ca807 __CFRunLoopDoSource1 + 215
- 19 CoreFoundation 0x023c7a93 __CFRunLoopRun + 979
- 20 CoreFoundation 0x023c7350 CFRunLoopRunSpecific + 208
- 21 CoreFoundation 0x023c7271 CFRunLoopRunInMode + 97
- 22 UIKit 0x002bcc6d -[UIApplication _run] + 625
- 23 UIKit 0x002c8af2 UIApplicationMain + 1160
- 24 XML 0x00002090 main + 102
- 25 XML 0x00002021 start + 53
- )
- terminate called after throwing an instance of 'NSException'
Wie zu sehen ist hat er aber anscheinend den wert aus der XML ausgelesen?
Hat jemand eine idee?