Created
May 4, 2012 06:27
-
-
Save rschumm/2592598 to your computer and use it in GitHub Desktop.
ObjC XML Parsing to CoreData Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<bestellung> | |
<vollerName>Hans Muster</vollerName> | |
<strasseNr>Muster. 31</strasseNr> | |
<plzOrt>8406 Winterthur-Töss</plzOrt> | |
<telephon>052 222 2222</telephon> | |
<email>[email protected]</email> | |
<bemerkung>Diese Bestellung kommt von XML.</bemerkung> | |
<bilder> | |
<bild> | |
<anzahl>1</anzahl> | |
<format>17x6</format> | |
<name>Bild_3.tiff</name> | |
<preis>6.00</preis> | |
</bild> | |
<bild> | |
<anzahl>1</anzahl> | |
<format>12x3</format> | |
<name>am_See.jpg</name> | |
<preis>123.00</preis> | |
</bild>ame | |
<bild> | |
<anzahl>1</anzahl> | |
<format>17x6</format> | |
<name>hans.jpg</name> | |
<preis>6.00</preis> | |
</bild> | |
<bild> | |
<anzahl>2</anzahl> | |
<format>12x45</format> | |
<name>chorA.jpg</name> | |
<preis>6.00</preis> | |
</bild> | |
</bilder> | |
</bestellung> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(NSManagedObject *)bestellung { | |
if (bestellung != nil) { | |
return bestellung; | |
} | |
NSManagedObjectContext * moc = [self managedObjectContext]; | |
NSError * fetchError = nil; | |
NSArray * fetchResults; | |
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; | |
@try { | |
NSEntityDescription * entDesc = [NSEntityDescription entityForName:@"Bestellung" inManagedObjectContext:moc]; | |
[fetchRequest setEntity:entDesc]; | |
fetchResults = [moc executeFetchRequest:fetchRequest error:&fetchError]; | |
} | |
@finally { | |
[fetchRequest release]; | |
} | |
if ((fetchResults != nil) && ([fetchResults count] == 1) && (fetchError == nil)) { | |
//Dept. versorgen und zurückgeben. | |
[self setBestellung:[fetchResults objectAtIndex:0]]; | |
return bestellung; | |
} | |
//error Zeugs ausgelassen, siehe Seite 31 | |
//Das ObjC ist halt doch auch nicht so Elegant. NSError und NSException? Was jetzt? | |
return nil; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Mail wird auf das Fenster gedragt. | |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender | |
{ | |
//NSLog(@"draging ausführen"); | |
NSPasteboard * pg = [sender draggingPasteboard]; | |
NSString * input = [pg stringForType:NSStringPboardType]; | |
NSError * err = nil; | |
//NSLog(input); | |
NSXMLDocument * dom = [[NSXMLDocument alloc] initWithXMLString:input options:NSXMLDocumentTidyXML error:&err]; | |
//NSLog([dom XMLStringWithOptions:NSXMLNodePrettyPrint]); | |
//Daten: | |
[self addDomValue:@"name" toValueKey:@"vollerName" fromDom:dom]; | |
[self addDomValue:@"adresse" toValueKey:@"strasseNr" fromDom:dom]; | |
[self addDomValue:@"ort" toValueKey:@"plzOrt" fromDom:dom]; | |
[self addDomValue:@"telefon" toValueKey:@"telephon" fromDom:dom]; | |
[self addDomValue:@"email" toValueKey:@"email" fromDom:dom]; | |
NSData * bemerkung = [[[[[dom rootElement] nodesForXPath:@"//bemerkungen" error:&err] objectAtIndex:0] stringValue] dataUsingEncoding:NSUTF8StringEncoding]; | |
[self setValue:bemerkung forKeyPath:@"bestellung.bemerkung"]; | |
//Bilder: | |
//iterieren | |
//neues Bild-Objekt herstellen | |
//hinzufügen | |
//Alle <format> durchgehen, dann im Parent schauen, was für ein Bild es war. | |
NSArray * bilderNodeList = [[dom rootElement] nodesForXPath:@"//format" error:&err]; | |
int i; | |
NSManagedObject * bild; | |
NSXMLElement * bildElement; | |
NSNumberFormatter * nf = [[NSNumberFormatter alloc] init]; | |
NSString * bertFormat; | |
for (i = 0; i < [bilderNodeList count]; i++) { | |
bildElement = [bilderNodeList objectAtIndex:i]; | |
bild = [NSEntityDescription insertNewObjectForEntityForName:@"Bild" inManagedObjectContext:[self managedObjectContext]]; | |
[bild setValue:[[[[bildElement nodesForXPath:@"../../name" error:&err] objectAtIndex:0] stringValue] stringByDeletingPathExtension]forKeyPath:@"name"]; | |
bertFormat = [[[bildElement nodesForXPath:@"name" error:&err] objectAtIndex:0] stringValue]; | |
//Tokenzier für Auseinandernehmen der Werte von <format/name>. | |
NSArray * tokens = [bertFormat componentsSeparatedByString:@" "]; | |
[bild setValue:[tokens objectAtIndex:0] forKeyPath:@"format"]; | |
[bild setValue:[tokens objectAtIndex:1] forKeyPath:@"farbe"]; | |
[bild setValue:[tokens objectAtIndex:2] forKeyPath:@"papier"]; | |
[bild setValue:[nf numberFromString: [[[bildElement nodesForXPath:@"anzahl" error:&err] objectAtIndex:0] stringValue] ] forKeyPath:@"anzahl"]; | |
[bild setValue:[nf numberFromString:[[[bildElement nodesForXPath:@"preis" error:&err] objectAtIndex:0] stringValue] ] forKeyPath:@"preis"]; | |
[bild setValue:[[[bildElement nodesForXPath:@"../../galerie" error:&err] objectAtIndex:0] stringValue] forKeyPath:@"galerie"]; | |
[bild setValue:[self bestellung] forKeyPath:@"bestellung"]; | |
} | |
//Optionen zur Bestelleung: | |
//iterieren | |
//TODO das gleiche nochmals für die <optionen>. | |
[nf release]; | |
[dom release]; | |
return YES; | |
} | |
//utility für mich. | |
// sucht "//domkey" im DOM und fügt den Wert als bestellung.cdkey dem Dokument hinzu. | |
- (void)addDomValue:(NSString*) domkey toValueKey:(NSString*) cdkey fromDom:(NSXMLDocument*) dom | |
{ | |
NSError * err; | |
NSString * xpath = [@"//" stringByAppendingString:domkey]; | |
NSString * wert = [[[[dom rootElement] nodesForXPath:xpath error:&err] objectAtIndex:0] stringValue]; | |
//NSLog(wert); | |
if (err != nil) { | |
NSLog(@"fehler in xpath: %@", err); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment