Parsing HTML with XPath in Objective-C - iphone

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.

Related

grave in the Go Language

After looking around for a while I was able to understand how the json: tags are used in the Go language. However two tags I have come across I'm still lost on, and can't seem to find documentation on it.
Both pertain to a REST api service and the full code can be found here-> code.google.com
What is the root: tag used for
gorest.RestService `root:"/orders-service/" consumes:"application/json" produces:"application/json"`
as well how does the method: tag work?
userDetails gorest.EndPoint `method:"GET" path:"/users/{Id:int}" output:"User"`
I didn't know if anyone had any links to a site or document that might explain this more, from the examples I can learn enough to use it. However, I would really like to fully understand it.
Thanks for your time!
Tags are nothing but strings, they don't have any meaning per-se.
Libraries can use reflection to introspect struct fields and interpret their tags. See reflect.StructTag.
In your case, gorest parses the following tags on Services:
root
consumes
produces
and these on Endpoints:
realm
method
path
output
input
role
postdata
Their meaning is described in gorest's documentation.
These are gorest tags. See gorest wiki http://code.google.com/p/gorest/wiki/GettingStarted

Where can I find an Objective C code which parses any XML file without knowing before any tag or attribute?

I would like to find a sample of code written in Objective C for iPhone which can parse any XML file, even if we don't know tags or attributes. Does anyone has something like that?
Probably you can convert the XML into a NSDictionary which then can be used at your ease.
I have not used this code, but maybe you can try this to convert your xml into dictionary
http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

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

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]);
}
}

XML Parser on MonoTouch?

I want to parse XML formatted string. How to use XML parser on MonoTouch?
Exactly the same way you would in standard C#.
Your options include:
XmlSerializer - good if you want to translate a full document to a set of C# objects
XmlDocument - good if the document is custom beyond XmlSerializer can handle
XPath - good for pulling out small pieces of data, if you don't care about the whole doc.
Linq2XML - another option using Linq.
Depending on what exactly you need.
You can use LINQ to XML in MonoTouch.
So,
var element = XElement.Parse("<cat>dog</cat>");
Console.WriteLine(element.Value);
prints "dog".
You canĀ“t use System.Xml.Linq in full, there will be a JIT part that will blow up when testing in the device, see Xamarin Monotouch limitations:
link

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]);