I have a web server which returns an XML file. Lets say http://www.foo.bar/foo.php?wantXML=1
How would I fetch that file from the server and then parse it to access the data? I guess I would have to spawn a new thread and do the whole thing in the background to not block the UI? What classes must I look at?
SeismicXML sample from Apple is exaclty what you are looking for.
You can attempt to do:
String manipulations
XPATH
Id opt for xpath in all but the most simple cases - and even in those, you can't argue against xpath. Im not sure of the libraries though, but I know libXML is written in C and supported on the iphone.
For string manipulations, you can use the NS* family of methods.(substringFromIndex, substringToIndex, etc)
And don't forget: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
Big Nerd Ranch, 'Parsing XML in Cocoa': http://weblog.bignerdranch.com/?p=48
To pull down XML or other stuff over http I recommend looking into using ASIHTTPRequest
Feel free to have a look at my convenient classes I created to parse simple XML documents like you can get from Nike+. Link
Basically the usage is as follows
NameValueParser *parser = [NameValueParser parser];
[parser addFieldName:#"screenName"]; // Name
[parser addFieldName:#"rank"]; // Position
[parser addFieldName:#"progress"]; // Distance
[parser parseData:data];
NSLog(#"%#", [parser list]); // Lets see what we got
I've used two approaches: NSXMLParser for simple and small files, and libxml for larger files. But there are libraries such as TouchXML that can simplify the process as well.
Basically, if you have a small data set, in memory DOM processing can work fine. But in a device such as the iPhone, you're better off using SAX-based parsers such as libxml2.
When you need to load the data:
[self performSelectorInBackground:#selector(LoadYourData) withObject:nil];
will not block the main UI thread.
For libxml2, you will need to implement C callbacks to process the chunks of data coming in from a NSURLConnection.
Related
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.
I have an array of objects that consist of several strings, I need to push these up to a web service as XML data. What are the steps involved here? As far as I know I need to convert the objects to a plist file, then convert this file to NSData (?) I can't find anything online that really lays it out..
Any help would be greatly appreciated.
Another approach is JSON. SBJSON is very common and simple to use.
It's a two-liner to get a json string from your array. Then you'll create an NSURLRequest that represents the post and an NSURLConnection that performs the request. Lot's of resources for that on SO and elsewhere.
You can try this or this. I doubt plist files are what you want. All you need is to serialize your object into the xml format your web service requires.
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
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.
I want to create a file using Objective-C, which stores the data comes from XML. I also have to do basic functions of read and write into that file. How can I do this?
You can parse a custom schema using the NSXMLParser class. This is especially useful since the NSXMLDocument class unfortunately does not exist on the iPhone. Thankfully, NSXMLParser is pretty easy to use. I've written an RSS feed parser using NSXMLParser in under half an hour.
If you have some flexibility over the XML structure, you could look at using the in-built commands to load and save a dictionary or array from/to a file, such as:
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:myFileName];
This will load in an XML file into a dictionary. It expects the file to be in the format
<key>Object 1</key><string>ContentsOfObject1</string>
<key>Object 2</key><string>ContentsOfObject2</string> etc
(You can also init an array in the same way. The file is simpler, basically just leaving out the "key" part).
Then, to save it you just use the following command:
[self.myArrayorMyDictionary writeToFile:fileFullName atomically:YES];
Hope that helps!