Xpath problem in iPhone - iphone

In GdataXML I can only use nodesforXpath from the root of XML, but I want that once I have the inferenceMembers I would like to apply Xpath to rest of this node, but not whole DOM tree, is that possible?
Example below works wrong in for loop, brings all the varibles in the DOM, but I want to work only on the variableElement not whole the DOM once I have the variableElement.
NSArray *inferenceMembers = [doc nodesForXPath:#"//inferenceresponse/state/variable[not(valuedefinition/variablevalue)]" error:nil];
for (GDataXMLElement *variableElement in inferenceMembers) {
Variable *variable=[[Variable alloc] init];
NSArray *items = [variableElement nodesForXPath:#"//variable/domaindefinition/domain/enumType/domainitem" error:nil];
}
The reason I want this not because it is hard to do, but I guess it would be slower if I xpath query to whole XML to reach the same element's children each time. I have read that I can do some magic with namespaces and NSDictionary but do not know how to do

Try this:
NSArray *inferenceMembers = [doc nodesForXPath:#"//inferenceresponse/state/variable[not(valuedefinition/variablevalue)]" error:nil];
for (GDataXMLElement *variableElement in inferenceMembers) {
Variable *variable=[[Variable alloc] init];
NSArray *items = [variableElement nodesForXPath:#"domaindefinition/domain/enumType/domainitem" error:nil];
}
It'd be useful to see the XML structure to be able to help fully.

Related

Looping through a plist containing an array of dictionaries

I have a plist containing an array with three elements all of which are dictionaries. These dictionaries contain four items each (latitude, longitude, title, subtitle). I want to loop through each of the elements and get the latitude and longitude (both of which are attributes of the dictionary).
The following code is used.
- (void)loadAnnotations{
//retrieve path of plist file and populate relevant types with its information
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSLog(#"hi inside method",branchList.lastObject);
self.branchList =[NSMutableArray array];
//code ok until this
for (NSDictionary *key in branchList)
{ NSLog(#"hi in loop");
PlaceFinderAnnotation *placeAnnotations = [[PlaceFinderAnnotation alloc] init];
//loop through annotations array, creating parking annotations filled with the information found in the plist
CLLocationDegrees latitude = [[key valueForKey:#"latitude"]floatValue];
CLLocationDegrees longitude = [[key valueForKey:#"longitude"]floatValue];
placeAnnotations.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.branchList addObject:placeAnnotations];
[placeAnnotations release]; placeAnnotations= nil;
objectForKey:#"subtitle"]];
}
}
The problem is it doesnt go into the loop. meaning it doesnt print out the log command "hi inside the looppp".
Let's assume this code succeeded (we have to assume because you don't appear to be checking to make sure). Let's also assume (because you don't say) "branchList" is an instance variable in the current class:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];
This hopefully leaves you with an array. You could of course eliminate the "hopefully" by ... checking to make sure it leaves you with an array ( if (branchList)... ). Then, since "branchList" seems to be an instance variable, you immediately blow it away by replacing it with an empty array (using an accessor rather than setting it directly as you did above):
self.branchList =[NSMutableArray array];
...so then you try to iterate an empty loop (so the NSLog() statement is never executed).
self.branchList =[NSMutableArray array];
creates an empty array and that is what the for statement is asked to loop through.
Delete that statement.
Perhaps this is what you want:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Places1" ofType:#"plist"];
self.branchList = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(#"hi inside method",branchList.lastObject);
for (NSDictionary *key in self.branchList) {
NSLog(#"hi inside loopppp");
if([branchlist count]){
for (NSDictionary *key in self.branchList) {
NSLog(#"Key item in branchlist %#",key);
}
}else{
NSLog(#"There is no items in branchlist");
}

iphone problem with KIssXML parser

I need quick help
structure of xml is like this
<VacancyList generated="2011-08-26T09:06:13" xsi:schemaLocation="http://www.abc.com/dtd/vacancy-list.xsd"><Vacancy id="157890" date_start="2010-10-12" date_end="2011-12-31" reference_number=""><Versions><Version language="nb">
and I am using KissXML like this
DDXMLDocument *theDocument = [[DDXMLDocument alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://pwc.easycruit.com/export/xml/vacancy/list.xml"]] options:0 error:&error];
NSArray* resultNodes = nil;
resultNodes = [theDocument nodesForXPath:#"Versions" error:&error];
but results is always blank.
Please help me with this
XML is case sensitive. You are looking for "versions" in your xpath query and not "Versions" (which is what exists in your document). I think that is your problem.

Extracting specific part of JSON respose on iPhone

I'm looking at adding a distance calculator to my application. I have been looking at Google's API put i cant seem to decode the JSON. I have managed to do so with PHP. The code for that was:
substr($convertedtoarray['routes']['0']['legs']['0']['distance']['text'], 0, -3);
On the iPhone i managed to get the JSON response but can't get the specific part of it that I want.
Json address: http://maps.googleapis.com/maps/api/directions/json?origin=plymouth&destination=pl210bp&sensor=false
NSMutableDictionary *luckyNumbers = [responseString JSONValue];
[responseString release];
if (luckyNumbers != nil) {
NSString *responseStatus = [luckyNumbers objectForKey:#"routes"];'
}
Where would I go from here?
Any help would be great cheers
NSString *responseStatus = [[[[[[luckyNumbers objectForKey:#"routes"]objectAtIndex:0] objectForKey:#"legs"]objectAtIndex:0] objectForKey:#"distance"] objectForKey:#"text"];
Very ugly you can extract in separate objects like this:
NSArray *routesArray = [luckyNumbers objectForKey:#"routes"];
NSDictionary *firstRoute = [routesArray objectAtIndex:0];
NSArray *legsArray = [firstRoute objectForKey:#"legs"];
NSDictionary *firstLeg = [legsArray objectAtIndex:0];
NSDictionary *distanceDict = [firstLeg objectForKey:#"distance"];
NSString *distanceText = [distanceDict objectForKey:#"text"];
Good luck.
The easiest thing to do would be to create a dictionary iterator, and loop over what children the luckynumbers dictionary has, you can print out, or debug, to see what the keys for these children are, and what object types they are.
I used this technique a few times to figure out what the structure of an XML doc I was being returned was like.

json string into key value NSArray iPhone

I get back a json of structure like
{ ResultCount =7; ResultLimit=30; ResultList=({ AlbumId=111;ArtistId=203},{AlbumId=112;ArtistId=203}); Status=0}
The ResultList is an array. How can I get the AlbumId and ArtistId in an NSArray?
Hi happy_iphone_developer,
ResultList is not a Array and it's NSDictionary.
NSString *urlDataString = [[NSString alloc] initWithData:RecievedData encoding:NSUTF8StringEncoding];
parser = [[SBJSON alloc] init];
NSError *error = nil;
NSArray *resultArray = [parser objectWithString:urlDataString error:&error];
NSString *extractString = [[resultArray valueForKey:#"ResultList"] valueForKey:#"AlbumId"];
Whatever you want to extract, use this way to extract the particular data.
Thanks.
are you wanting to make good json ? then,
{ ResultCount:7, ResultLimit:30, ResultList:[{ AlbumId:111,ArtistId:203},{AlbumId:112,ArtistId:203}]}
or you want to parse in iPhone then
http://code.google.com/p/json-framework
is good framework
Just use a JSON framework like this and you're set.
The ResultList is not a NSArray, it's a NSDictionnary.

how to load array of content to dictionary type

i have a array with data,i want to load the dictiionary type element with array......
NSArray *thisArray = [[NSArray alloc] initWithContentsOfFile:path];
NSSdictionary *state;
how to load the content with array....
any help appreciated...
The usual pitfall is getting the path right for the file, other than that it is pretty straight forward... if I understand the question correctly
NSString* path = [[NSBundle mainBundle]pathForResource:#"someDataFile" ofType:#"plist"];
NSDictionary *state = [[NSDictionary alloc]initWithContentsOfFile:path];