0

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] initWithData:connection.data options:0 error:nil];

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

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

thanks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Piero
  • 9,173
  • 18
  • 90
  • 160

1 Answers1

1

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":

<day>
Monday
</day>
<day>
Tuesday
</day>
...
<day>
Friday
</day> 

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

jmstone617
  • 5,707
  • 2
  • 24
  • 26
  • 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 Apr 03 '12 at 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 Apr 03 '12 at 22:36
  • no sorry :), thank you for the answer :), but i have to add the lib? – Piero Apr 03 '12 at 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 Apr 03 '12 at 22:41
  • hello again :), if i want retrieve the Time in this xml what is the xpathquery here? – Piero Apr 03 '12 at 23:11