how to provide name-space to use xpath query in objective-c - iphone

i am trying to use xpath-query in my app for xml parsing. my work is almost completed upto name space concept came into picture.i am always writing query correctly but its returning me the 0 objects in my array.i tried many blogs and also many posts in our stackoverflow.com but none of them not working..i hope my problem need small modification can any one modify my code.my code
Thanks All.
I think many people try to show my question is duplicate but it is different bec none of the questions not worked for my problem.please provide me your suggestion.your answer more help full for my work.

I take it you're using libxml along with Matt Gallagher's Cocoa wrappers for it? (It's helpful to put which third party libraries you're using in your question.)
When I did this, I modified Matt's code so that PerformXPathQuery took an additional argument, an NSDictionary of namespaces. (The other functions such as PerformXMLXPathQuery also took this argument, and pass through the object.)
Then, in PerformXPathQuery, after creating the context, I registered the namespaces thus:
if (namespaces != nil)
{
for (NSString *key in namespaces)
{
xmlXPathRegisterNs(xpathCtx, (const xmlChar *)[key UTF8String], (const xmlChar *)[[namespaces objectForKey:key] UTF8String]);
}
}

Related

Parsing XML using GDataXML returns a NSString object with a retainCount of 95.How to ensure it gets released?

The NSString object that is got by the following code has got a retainCount of 95.
for(GDataXMLElement *ele in [doc.rootElement elementsForName:#"myKey"])
{
NSLog(#"myKey %d",[[[ele.children objectAtIndex:0] stringValue] retainCount]);
[myDict setObject:[[ele.children objectAtIndex:0] stringValue] forKey:#"myKey"];
}
. so would it get released later when
[myDict removeAllObjects];
[myDict release];
is called.
the problem i am facing is that i have hundreds of strings like this parsed.... and all their retaincounts are around 95...would these strings get released?
the problem i am facing is that i have hundreds of strings like this
parsed.... and all their retaincounts are around 95...would these
strings get released?
First, retainCount is useless. Don't call it. No, really, don't use retainCount.
To answer your question, look to Instruments. Do the objects that you expect to go away actually stay in memory? If so, then turn on reference count tracking and see what still holds references to them (or what retains are unbalanced).
More likely than not, the XML subsystem is unique-ifying the strings such that only one copy of what may be repeated hundreds of times is in memory. That one copy may be retained dozens or hundreds of times as a result. When you removeAllObjects from myDict, there may still be a reference to the objects. It might even be an autoreleased reference and, thus, will actually go away.
The only way to know is to look to see if the objects are deallocated via Instruments (or some other means).
As per the definitions & explanations given by many others including raywinderlich in his How To Choose The Best XML Parser for Your iPhone Project.
GDataXML is nothing but a NSXML style DOM XML parser for the iPhone, developed by Google as part of their Objective-C client library. Consisting of just a M file and a header, it supports both reading and writing XML documents and XPath queries.
A DOM parser reads the entire document and builds up an in-memory representation that you can query for different elements. Often, you can even construct XPath queries to pull out particular pieces.
So it just creates a tree like structure for the given entire XML, each elements can be queried to pull particular pieces.
And as we know this all works with the pointers, so what ever elements we get from this tree will be just pointing to that object, with the same name(used while initializing xml) or a new name while pulling particular pieces(using NSXMLElement).
And so everything will be retained until we release the XMLDoc while initializing XML.
If you want we can check the retain counts after releasing the XMLDoc(but it may give crash as are we using released object).
I think it works in this way, if you or other developers have any other info on this share your info.

Frank: Is it possible to write step definitions in Objective-C

How could I write step definitions in Objective-C? E.g.:
Given(#"^the address book is empty$", ^{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < CFArrayGetCount(people); i++) {
ABAddressBookRemoveRecord(addressBook,
CFArrayGetValueAtIndex(people, i), NULL);
}
});
The example above was derived from Rob Holland's blog post "BDD on iPhone: iCuke".
Why would I want to do such a thing? Because, as much as I love Ruby, I prefer to develop iOS apps in Objective-C and to write tests in the same language as that of the app I'm testing. Also, this would allow me to do low-level things, like erasing the address book or editing other data with Core Data.
Can I name the features directory Features with a capital "F"?
I responded to Matt's question on the Frank mailing list here
I agree; test your Objective-C applications in Objective-C. Personally, I use Cedar for this and would do it soup-to-nuts in Objective-C (no cuke), but I realize that might not be a very helpful answer. Not to hijack your thread, but did you find Frank to be any easier than Cedar to set up?
To attempt to answer your second question:
By default I think Cucumber looks for a 'features' directory. I don't know if it is case sensitive, but the fact that your asking means it probably is. You can specify the directory when you run Cucumber.
eg. a cucumber target in my Rakefile with non-standard location
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = ["--format pretty", "FunctionalTests/Frank"]
end

Parsing HTML with XPath in Objective-C

Hey guys, I'm trying to parse HTML with XPath from http://lib.harvard.edu/libraries/hours.html in Objective-C for an application that shows the operating hours for each day of the week at each of the 50 libraries listed on the website. I found code to facilitate XPath parsing of HTML in Objective-C at cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html, but I'm still a little confused about how I should go about obtaining the hours for each day for each library. The relevant method to use seems to be
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query)
and my code so far is
NSURL *urlPath = [NSURL URLWithString:#"http://lib.harvard.edu/libraries/hours.html"];
NSArray *array = PerformHTMLXPathQuery([NSData dataWithContentsOfURL:urlPath], NSString *query);
but, since I've never used XPath before, I'm not sure what string I should use in the second parameter of the method. Does anyone have any ideas?
Also, I'm not quite sure what to do with the array that gets returned by PerformHTMLXPathQuery(). I feel like cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html gives a pretty good explanation, it's just that I've never used XPath before so it doesn't make much sense to me at this point. So, to summarize, as long as my code so far is correct, I want to know what to use for the second parameter in the PerformHTMLXPathQuery() method and how to extract the relevant data from the array it returns. Any help would be much appreciated!
XPath is a language for navigating XML documents. The query parameter is an XPath query string, which you hope will be able to extract the elements you want from the HTML file. I say "hope" because
I don't know how well XPath plays with HTML 4 documents
I've had a look at the source of the page you want to parse and it is quite complex.
Anyway, those points aside, you'll be wanting to learn how to create an XPath expression. Fortunately, Google is your friend and typing "XPath" into it brings up the W3Schools tutorial on XPath. I have only skimmed it but it looks like what you need.

iphone compare   with NSString

i was comparing A string coming from XML with another String and results were showing that they are not equal. but in NSLog() both were same ( e.g. Valore Books ).
then i checked the Source of the XML and i came to know that the actual string is "Valore Books" and   is infact a space. but the problem is this when i am comparing it with #"Valore Books", it is saying both are not same.
What to Do ??
Note: I'm replacing my original answer with one that's actually correct for this problem. Sorry for the initial misunderstanding.
The following line will unescape the html entities in your string.
NSString *A = #"Valore Books";
NSString *B = (NSString *)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)A, NULL);
I couldn't find any equivalent function that was higher level, but the performance of this should be excellent. If I read the docs correctly, you can pass in a CFDictionaryRef as the third argument to specify extra conversions, but it seems that this does a good job doing standard ones on it's own.
Docs are here.
Note that it's probably a good idea to handle the encoding whereever you're pulling those strings into your program at, and not everytime you're comparing.
Also found a second part of this you need to consider. &#160 isn't just a space, it's a non breaking space, which the above code converts to \312 instead of the standard space. Those are in fact separate characters in the encoding and when you do a string compare it will fail.
Maybe it'd be easiest to replace #160 with #32 using
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
and then running it through the unescape.
It also just occurred to me that the CFXMLCreateStringByUnescapingEntities won't be available on the iphone. Here is a link to an example that shows how to do similar conversions on the iphone.
  is a non-breaking space (Unicode value U+00A0)
The regular space is (in #"Valore Books") has Unicode value U+0020.
So it is not the same character, and the two strings are not equal.
i solved the problem using a string parsing utility provided by google. Thanks everybody.

Alternative for print_r in php for iphone(objective-c)?

I've started with objective-c/iphone programming a couple of days ago, and still having some problems converting from other languages.. I'm currently testing around with NSArrays, and NSMutableArrays, and it's really annoying that I haven't found any method for effectively displaying the contents of my arrays (without creating loops and displaying row by row).
Is there some easy method like print_r in PHP or console.log in javascript(w/Firefox && firebug) ?
Really appreciate any help :)
Use NSLog, e.g.:
NSLog(#"This is my NSArray: %#", [myNSArray description]);