Re: XML parser for objective C
Re: XML parser for objective C
- Subject: Re: XML parser for objective C
- From: Peter Hall <email@hidden>
- Date: Fri, 30 Sep 2005 13:16:00 -0400
Here's a quick example both are the equivilent 1 uses the cocoa stuff the
other uses libxml2 directory
------ libxml2 example -----
- (IBAction)ItemLookup:(id)sender
{
if (strlen(amazon_id)==0)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"You have do not have an
Amazon Subscriber ID (hint)amazon_id.h?"
defaultButton:@"Oops" alternateButton:nil otherButton:nil
informativeTextWithFormat:@"You have not configured the software."];
[alert runModal];
return;
}
NSArray *arobjs = [parcontroller arrangedObjects];
NSLog(@"objs=%@",arobjs);
NSLog(@"indexed=%@, id=%d, %@",pselecteditemindex, [pselecteditemindex
firstIndex], [parcontroller selectionIndexes]);
NSDictionary *ttt = (NSDictionary*)[ arobjs
objectAtIndex:[[parcontroller selectionIndexes] firstIndex]];
NSLog(@"ttt=%@",ttt);
NSArray *ptheasin=[ttt valueForKey:@"asin"];
NSLog(@"ptheasin = %@",ptheasin);
// NSString *ptrial=[NSString stringWithFormat:@"ha ha %@",[ptheasin
objectAtIndex:0] ];
NSString *ptheurl=[NSString
stringWithFormat:@"http://webservices.amazon.com/onca/xml?Service=AWSECommer
ceService&SubscriptionId="amazon_id"&Operation=ItemLookup&ItemId=%@&Response
Group=ItemAttributes,Images,Medium",ptheasin];
//NSError *perr;
NSURL *purl = [NSURL URLWithString:ptheurl];
NSString *src_doc=[[NSString alloc] initWithContentsOfURL:purl
/*encoding:NSUTF8StringEncoding error:&perr*/];
char *ssrcdoc = (char*)calloc(1,[src_doc cStringLength]+1);
[src_doc getCString:ssrcdoc];
NSLog(@"xpath 0 %@",src_doc);
xmlDocPtr doc=xmlParseMemory(ssrcdoc,strlen(ssrcdoc));
xmlXPathContextPtr xctx=xmlXPathNewContext(doc);
// previous value
"http://webservices.amazon.com/AWSECommerceService/2005-03-23"
xmlXPathRegisterNs(xctx, (xmlChar*)"pih",
(xmlChar*)"http://webservices.amazon.com/AWSECommerceService/2005-07-26");
xmlXPathObjectPtr tmpnode;
//NOTE need to put clean up of each xpath statement in. xmlobjectfree or
something like that
NSLog(@"xpath 1");
tmpnode =
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:MediumImage/pih:URL/text()"
,xctx); //"/*/Items/Item/MediumImage/URL/text()"
NSString *pimagepath=(tmpnode && tmpnode->nodesetval) ?[NSString
stringWithUTF8String:(const char*)tmpnode->nodesetval->nodeTab[0]->content]:
@"";
xmlXPathFreeObject(tmpnode);
NSLog(@"path=%@",pimagepath);
[self setImage:pimagepath];
NSLog(@"xpath 2");
tmpnode=
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:ItemAttributes/pih:Audience
Rating/text()",xctx);
NSString *audiencerating=(tmpnode && tmpnode->nodesetval) ?[NSString
stringWithUTF8String:(const char*)tmpnode->nodesetval->nodeTab[0]->content]:
@"";
xmlXPathFreeObject(tmpnode);
NSLog(@"xpath 3");
tmpnode =
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:ItemAttributes/pih:RunningT
ime/text()",xctx);
NSString *runningtime=(tmpnode && tmpnode->nodesetval) ?[NSString
stringWithUTF8String:(const char*)tmpnode->nodesetval->nodeTab[0]->content]:
@"";
xmlXPathFreeObject(tmpnode);
NSLog(@"xpath 4");
tmpnode =
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:ItemAttributes/pih:RunningT
ime/@Units",xctx);
NSString *runningtimeunits=(tmpnode && tmpnode->nodesetval!=0 &&
tmpnode->nodesetval->nodeTab!=0) ?[NSString stringWithUTF8String:(const
char*)tmpnode->nodesetval->nodeTab[0]->children->content]: @"";
NSString *playtime=[NSString stringWithFormat:@"%@ %@",runningtime,
runningtimeunits];
xmlXPathFreeObject(tmpnode);
NSLog(@"xpath 5");
tmpnode =
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:ItemAttributes/pih:Director
/text()",xctx);
NSString *pdirector = (tmpnode && tmpnode->nodesetval) ?[NSString
stringWithUTF8String:(const char*)tmpnode->nodesetval->nodeTab[0]->content]:
@"";
xmlXPathFreeObject(tmpnode);
NSLog(@"xpath 6");
tmpnode =
xmlXPathEval((xmlChar*)"//pih:Items/pih:Item/pih:DetailPageURL/text()",xctx)
;
NSString *pdetails = (tmpnode && tmpnode->nodesetval) ?[NSString
stringWithUTF8String:(const char*)tmpnode->nodesetval->nodeTab[0]->content]:
@"";
NSMutableArray *trump= [NSMutableArray arrayWithArray:[parcontroller
selectedObjects]];
xmlXPathFreeObject(tmpnode);
NSLog(@"xpath 1");
[trump setValue:playtime forKey:@"runningtime"];
[trump setValue:pdirector forKey:@"director"];
[trump setValue:audiencerating forKey:@"audiencerating"];
[trump setValue:pdetails forKey:@"amazon"];
[parcontroller setSelectedObjects:trump];
xmlXPathFreeContext(xctx);
xmlFreeDoc(doc);
free(ssrcdoc);
}
------------------- cocoa example --------
- (IBAction)ItemLookup:(id)sender
{
if (strlen(amazon_id)==0)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"You have do not have an
Amazon Subscriber ID (hint)amazon_id.h?"
defaultButton:@"Oops" alternateButton:nil otherButton:nil
informativeTextWithFormat:@"You have not configured the software."];
[alert runModal];
return;
}
NSLog(@"indexd=%@",pselecteditemindex);
NSDictionary *ttt = (NSDictionary*)[[parcontroller arrangedObjects]
objectsAtIndexes:pselecteditemindex];
NSArray *ptheasin=[ttt valueForKey:@"asin"];
// NSString *ptrial=[NSString stringWithFormat:@"ha ha %@",[ptheasin
objectAtIndex:0] ];
NSString *ptheurl=[NSString
stringWithFormat:@"http://webservices.amazon.com/onca/xml?Service=AWSECommer
ceService&SubscriptionId="amazon_id"&Operation=ItemLookup&ItemId=%@&Response
Group=ItemAttributes,Images,Medium",[ptheasin objectAtIndex:0]];
NSError *perr;
NSURL *purl = [NSURL URLWithString:ptheurl];
NSXMLDocument *doc=[[NSXMLDocument alloc] initWithContentsOfURL:purl
options:0 error:&perr];
NSArray *tmpnode;
tmpnode = [doc nodesForXPath:@"//Items/Item/MediumImage/URL" error:&perr];
NSString *pimagepath=([tmpnode count]) ? [[tmpnode objectAtIndex:0]
stringValue] : @"";
NSLog(@"path=%@",pimagepath);
[self setImage:pimagepath];
tmpnode= [doc
nodesForXPath:@"//Items/Item/ItemAttributes/AudienceRating" error:&perr] ;
NSString *audiencerating=([tmpnode count]) ? [[tmpnode
objectAtIndex:0] stringValue] : @"";
tmpnode = [doc
nodesForXPath:@"//Items/Item/ItemAttributes/RunningTime" error:&perr];
NSString *runningtime=([tmpnode count])?[[tmpnode objectAtIndex:0]
stringValue] :@"";
tmpnode = [doc
nodesForXPath:@"//Items/Item/ItemAttributes/RunningTime/@Units"
error:&perr];
NSString *runningtimeunits=([tmpnode count]) ?[[tmpnode
objectAtIndex:0] stringValue] : @"";
NSString *playtime=[NSString stringWithFormat:@"%@ %@",runningtime,
runningtimeunits];
tmpnode = [doc nodesForXPath:@"//Items/Item/ItemAttributes/Director"
error:&perr];
NSString *pdirector = ([tmpnode count])?[[tmpnode objectAtIndex:0]
stringValue] : @"";
tmpnode = [doc nodesForXPath:@"//Items/Item/DetailPageURL"
error:&perr];
NSString *pdetails = ([tmpnode count]) ? [[tmpnode objectAtIndex:0]
stringValue] : @"";
NSMutableArray *trump= [NSMutableArray arrayWithArray:[parcontroller
selectedObjects]];
[trump setValue:playtime forKey:@"runningtime"];
[trump setValue:pdirector forKey:@"director"];
[trump setValue:audiencerating forKey:@"audiencerating"];
[trump setValue:pdetails forKey:@"amazon"];
[parcontroller setSelectedObjects:trump];
}
> From: David Chan <email@hidden>
> Reply-To: David Chan <email@hidden>
> Date: Fri, 30 Sep 2005 17:53:11 +0800
> To: email@hidden
> Subject: XML parser for objective C
>
> Dear All,
> I've just got my hands on objective C and cocoa and at the moment, I was
> given task to parse some XML files using the objective C project I am
> developing to get some attribute from the XML. I was given a folder of XML
> parser library written in C, but I am not sure how to use that to parse the
> XML files. Is there any sample code out there which I can refer to so that I
> can have better idea of how to parse XML files from my objective C class?
> Thanks a lot.
>
> Regards
> David
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden