Get value of an element without parse all xml file in ios

Go To


i want know if is possible, to get a specific element value of a xml file without use all delegate NSXMLParse method, i know that in OS X is possible with two simple line:

    NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] options:0 error:nil];

    NSString *time = [[xmlDoc rootElement] stringValueForXPath:@"./Time"];

is possible do the same thing in iOS? with few line?


2012-04-03 21:52
by Piero
This uses XPath. You can use XPath on iOS, as well - jmstone617 2012-04-03 22:16
how i can use it? i have to add some libraries - Piero 2012-04-03 22:20


XPath exists for iOS, as well. Here is a really great tutorial on using libxml2, XPath, and XML for parsing data.

Coca with Love

With XPath, you're expected to pass in a clip query. For the following, XML, your clip query would be "//day":


This will give you an array of 5 dictionaries with node_name = day and node_content = the day of the week.

2012-04-03 22:24
by jmstone617
i read it, there are a lot of code, in Mac OS X there are only two line...and i have also to add the lib.. - Piero 2012-04-03 22:33
Well, I apologize if you want to write an app that involves two lines of code and expect it to do magic. Please realize that iOS is NOT OS X. If you want to do this in two lines, develop for OS X : - jmstone617 2012-04-03 22:36
no sorry :), thank you for the answer :), but i have to add the lib - Piero 2012-04-03 22:39
You do. But the lib is already included in xcode. You add it the same way you would add a frame work. Just scroll down and you'll see it. You just need to manually add the XPathQuery.h and .m files, which has very little overhead - jmstone617 2012-04-03 22:41
ok thank you very much for the answer : - Piero 2012-04-03 22:42
hello again :), if i want retrieve the Time in this xml what is the xpathquery here? Piero 2012-04-03 23:11
i have found it's //Tim - Piero 2012-04-04 00:04
I updated my answer for this question - jmstone617 2012-04-04 10:52