iphone xml parsen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • iphone xml parsen

    Hallo an alle : ),

    ich bin gerade am durchdrehen, versuche mein XML zu Parsen.
    Dies funktioniert auch beim einem einfachen aufbau wie diesen:

    XML-Quellcode

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <usr_web2_7>
    3. <kategorien>
    4. <kid>1</kid>
    5. <kategorie>aa</kategorie>
    6. <pikto>aa.png</pikto>
    7. </kategorien>
    8. <kategorien>
    9. <kid>2</kid>
    10. <kategorie>ab</kategorie>
    11. <pikto>ab.png</pikto>
    12. </kategorien>
    13. </usr_web2_7>
    Alles anzeigen


    jetzt versuche ich aber auch die Artikel und die Unterkategorien auszulesen:

    XML-Quellcode

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <usr_web2_7>
    3. <kategorien>
    4. <kid>1</kid>
    5. <kategorie>Arbeit und Wirtschaft</kategorie>
    6. <pikto>Arbeit_Wirtschaft.png</pikto>
    7. <ukat>
    8. <sub>Ausbildung</sub>
    9. <con>
    10. <eintrag>
    11. <url>http://www.will keine werbung machen.de/1.html/</url>
    12. <title>titel 1</title>
    13. <des>text 1 ...</des>
    14. </eintrag>
    15. <eintrag>
    16. <url>http://www.will keine werbung machen.de/2.html/</url>
    17. <title>titel2</title>
    18. <des>text 2 ...</des>
    19. </eintrag>
    20. </con>
    21. </ukat>
    22. <ukat>
    23. <sub>Börse</sub>
    24. <con>
    25. <eintrag>
    26. <url>http://www.will keine werbung machen.de/3.html/</url>
    27. <title>titel 3</title>
    28. <des>text 3 ...</des>
    29. </eintrag>
    30. </con>
    31. </ukat>
    32. </kategorien>
    33. </usr_web2_7>
    Alles anzeigen


    hier der code:

    Quellcode

    1. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    2. if ([elementName isEqualToString:@"usr_web2_7"]){
    3. //Initia. array.
    4. appDelegate.books = [[NSMutableArray alloc] init];
    5. }
    6. else if ([elementName isEqualToString:@"kategorien"]){
    7. //Initia. book.
    8. aBook = [[Book alloc] init];
    9. aBook.kid = [[NSMutableArray alloc] init];
    10. }
    11. else if ([elementName isEqualToString:@"ukat"]){
    12. aBook = [[Book alloc] init];
    13. aBook.kid = [[NSMutableArray alloc] init];
    14. }
    15. else if ([elementName isEqualToString:@"con"]){
    16. aBook = [[Book alloc] init];
    17. aBook.kid = [[NSMutableArray alloc] init];
    18. }
    19. else if ([elementName isEqualToString:@"eintrag"]){
    20. aBook = [[Book alloc] init];
    21. aBook.kid = [[NSMutableArray alloc] init];
    22. }
    23. NSLog(@"Element: %@", elementName);
    24. }
    25. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    26. if(!currentElementValue)
    27. currentElementValue = [[NSMutableString alloc] initWithString:string];
    28. else
    29. [currentElementValue appendString:string];
    30. NSLog(@"Value: %@", currentElementValue);
    31. }
    32. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
    33. namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    34. // ignoriere Root und leere Elemente
    35. if (( [elementName isEqualToString:@"usr_web2_7"]) || ( [elementName isEqualToString:@"ukat"] ) || ( [elementName isEqualToString:@"con"] ) || ( [elementName isEqualToString:@"eintrag"] ) )
    36. return;
    37. if([elementName isEqualToString:@"kategorien"]) {
    38. [appDelegate.books addObject:aBook];
    39. [aBook release];
    40. aBook = nil;
    41. }
    42. if([elementName isEqualToString:@"ukat"]) {
    43. [appDelegate.books addObject:aBook];
    44. [aBook release];
    45. aBook = nil;
    46. }
    47. if([elementName isEqualToString:@"con"]) {
    48. [appDelegate.books addObject:aBook];
    49. [aBook release];
    50. aBook = nil;
    51. }
    52. if([elementName isEqualToString:@"eintrag"]) {
    53. [appDelegate.books addObject:aBook];
    54. [aBook release];
    55. aBook = nil;
    56. }
    57. else
    58. [aBook setValue:currentElementValue forKey:elementName];
    59. [currentElementValue release];
    60. currentElementValue = nil;
    61. }
    Alles anzeigen


    Konsolen auszug:

    Quellcode

    1. 2010-10-30 15:24:40.665 XML[2559:207] Element: sub
    2. 2010-10-30 15:24:40.666 XML[2559:207] Value:
    3. Ausbildung
    4. 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.'
    5. *** Call stack at first throw:
    6. (
    7. 0 CoreFoundation 0x02488b99 __exceptionPreprocess + 185
    8. 1 libobjc.A.dylib 0x025d840e objc_exception_throw + 47
    9. 2 CoreFoundation 0x02488ad1 -[NSException raise] + 17
    10. 3 Foundation 0x000320f3 _NSSetUsingKeyValueSetter + 135
    11. 4 Foundation 0x00032061 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    12. 5 XML 0x00003719 -[XMLParser parser:didEndElement:namespaceURI:qualifiedName:] + 1008
    13. 6 Foundation 0x000f33a9 _endElementNs + 453
    14. 7 libxml2.2.dylib 0x02915ea7 xmlParseXMLDecl + 1346
    15. 8 libxml2.2.dylib 0x02920bb1 xmlParseChunk + 3984
    16. 9 Foundation 0x000f2baa -[NSXMLParser parse] + 321
    17. 10 XML 0x000021e3 -[XMLAppDelegate applicationDidFinishLaunching:] + 304
    18. 11 UIKit 0x002baf80 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252
    19. 12 UIKit 0x002bd3b0 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
    20. 13 UIKit 0x002c73ec -[UIApplication handleEvent:withNewEvent:] + 1958
    21. 14 UIKit 0x002bfb3c -[UIApplication sendEvent:] + 71
    22. 15 UIKit 0x002c49bf _UIApplicationHandleEvent + 7672
    23. 16 GraphicsServices 0x02d68822 PurpleEventCallback + 1550
    24. 17 CoreFoundation 0x02469ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    25. 18 CoreFoundation 0x023ca807 __CFRunLoopDoSource1 + 215
    26. 19 CoreFoundation 0x023c7a93 __CFRunLoopRun + 979
    27. 20 CoreFoundation 0x023c7350 CFRunLoopRunSpecific + 208
    28. 21 CoreFoundation 0x023c7271 CFRunLoopRunInMode + 97
    29. 22 UIKit 0x002bcc6d -[UIApplication _run] + 625
    30. 23 UIKit 0x002c8af2 UIApplicationMain + 1160
    31. 24 XML 0x00002090 main + 102
    32. 25 XML 0x00002021 start + 53
    33. )
    34. terminate called after throwing an instance of 'NSException'
    Alles anzeigen


    Wie zu sehen ist hat er aber anscheinend den wert aus der XML ausgelesen?
    Hat jemand eine idee?
  • Ich glaub ich habe nur eine pause gebraucht, weil ich den fehler gefunden habe : ).

    ganz schlechter Stil
    ok ? ich bin jetzt Frustriert ;) .
    Habe dies aus ein Tutorial und bei Appel Dev, habe ich auch eine ähnliche beschreibung gefunden.

    Kannst du da ein paar tips rausrücken :D .

    Danke schon mal für den Tip!