Handling complex xml with multiple delegates - objective-c

I'm currently trying to create a parser to populate my core data database. I have tried using NSXMLParser, where I realized that would be kinda difficult. Because I cant handle the xml document well enough.
Then I searched and found that I could use multiple delegates for handling complex documents.
I tried to find some tutorials/guides/examples for this. But I only examples with simple XML files.
I found this similar to mine. NSXMLParser with multiple delegates
But the guy doesn't get any answers.
Maybe some people here have some examples?
Thanks in advance.

The XML structure of the guy you mentioned is quite easy to parse, he most probably is doing it wrong in the code he didn't show. You do not need to use multiple delegates to parse an XML file. You've already said you've got some sort of data model in your app. You need to create an NSXMLParser subclass and temporary data model objects. Then listen which tag is currently being parsed and store the data in your temporary objects, saving them into the NSManagedObjectContext. Really its that simple. If that doesn't makes it clear, put your xml document in the question and I'll try to explain this with as much code as possible.

Related

Objective-C best choice for saving data

I'm currently looking for the best way to save data in my iPhone application; data that will persist between opening and closing of the application. I've looked into archiving using a NSKeyedArchiver and I have been successful in making it work. However, I've noticed that if I try to save multiple objects, they keep getting overwritten every time I save. (Essentially, the user will be able to create a list of things he/she wants, save the list, create a few more lists, save them all, then be able to go back and select any of those lists to load at a future date.)
I've heard about SQLite, Core Data, or using .plists to store multiple arrays of data that will persist over time. Could someone point me in the best direction to save my data? Thanks!
Core Data is very powerful and easy to use once you get over the initial learning curve. here's a good tutorial to get you started - clicky
As an easy and powerful alternative to CoreData, look into ActiveRecord for Objective-C. https://github.com/aptiva/activerecord
I'd go with NSKeyedArchiver. Sounds like the problem is you're not organizing your graph properly.
You technically have a list of lists, but you're only saving the inner-nested list.
You should be added the list to a "super" list, and then archiving the super-list.
CoreData / SQL seems a bit much from what you described.
Also you can try this framework. It's very simple and easy to use.
It's based on ActiveRecord pattern and allow to use migrations, relationships, validations, and more.
It use sqlite3 only, without CoreData, but you don't need to use raw sql or create tables manually.
Just describe your iActiveRecord and enjoy.
You want to check out this tutorial by Ray Wenderlich on Getting started with CoreData. Its short and goes over the basics of CoreData.
Essentially you only want to look at plists if you have a small amount of data to store. A simple list of settings or preferences. Anything larger than that and it breaks down specifically around performance. There is a great video on iTunesU where the developers at LinkedIn describe their performance metrics between plists and CoreData.
Archiving works, but is going to be a lot of work to store and retrieve your data, as well as put the performance challenge on your back. So I wouldn't go there. I would use CoreData. Its extremely simple to get started with and if you understand the objects in this stack overflow question then you know everything you need to get going.

Transforming Core Data info into XML and vice versa

I have my application talking to a server. What I need to do is pull information from the Core Data persistent store and parse it to XML, then send it to the server. The server will respond in XML and I will need to parse this back to Core Data.
What is the best way to do this? I'm aware of NSXMLParser but unfortunately I haven't found any good tutorials on how to use it for my application.
Edit: There are so many better things than NSXMLParser now. Check SO for more answers.
As far as writing your data out as XML goes, you'll probably find this Stackoverflow Question helpful.
Sending the data back and forth can be done using an NSURLConnection with a custom delegate.
Retrieving the data from the XML can be done using NSXMLParser, like you said.

Objective-C: Save Text. Best solution?

This may sound like a noobish question (in fact, it is), but I can't figure out what I should use to save between sessions:
NSStrings
NStextStorages
I've found some alternatives, but I don't know which is best for the case and why:
Core Data (or has it nothing to do?)
SQL
Edit: I have a simple interface that adds "posts" to a database (that doesn't exist yet, hence my question). Each "post" has a "title" one or two "authors" and a "body". While the "title" and the "authors" are plain strings (NSStrings), the "body" is rich text (NSTextStorage). But I don't want to save files, I want to generate a database that I could then use to format automatically and generate a PDF file (for me to print). I've been reading Core Data and it looks like the way to go, I'm just not sure how I could then convert my data and format it into a PDF.
Core Data would probably be great for what you're doing, although anything you do with Core Data you can probably do with SQL if you already know it.
Read the Core Data guides. Basically you lay out your schema and then can add or remove managed objects from the managed object context. Core Data is nice because it can do a lot of validation for free, and is good performance.
I was in a similar situation not too long ago.. and although im developing for iPhone, i belive the principle would be the same.. i found core data to be very useful, once i got to grips with it it proved to be quite useful..
if your saving a lot of information/data, than core data may be the way to go..
hope this helps :)

OS X Data Storage in PLIST Example or Tutorial?

I have looked for several hours for a good tutorial on how to store data for my command line program that I am writing in Objective-C. It seems like my options are NSUserDefaults, Plists, sqlite, or XML.
Now, I think at most I'd have 50kb of data to store, retrieve, and modify, so it seems like from what I read Plists would work just fine, but I'm having difficulty finding a good tutorial online. (I'm completely new to Objective C). Everything I find is for the iPhone, or NSUserDefaults - neither of which I'm looking for.
Can anyone point me in the right direction? I'm hoping just to replicate the very basic functionality of "defaults write com.yourcompanyname.yourappanme key data"
I would greatly appreciate a nudge in the right direction, but again, I'm completely new so the most basic tutorial is best.
The Property List Programming Guide is a good place to start.
The easiest way do do what you want is to put everything you want to save into an NSDictionary or NSMutableDictionary, and then use writeToFile:atomically: to save the data. You can read it back with initWithContentsOfFile:.
The property list guide linked above would rather you use NSPropertyListSerialization to generate an NSData object from the dictionary, and save that instead. That's a (little) bit more involved, but arguably a better approach.
You actually might want to look into using Core Data: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html
It will let you save data in XML format.

Simple way to parse XML to an tableview Objective C

For an iPhone application I need to request data from an online database. I choice to use for an restful web service. Main reason because it will connect to an JAVA server.
I did find a useful wrapper that helps me to get the data but now I need a good workaround to play with the data. The output is XML. If it is better to use JSON or something else please let me know.
In this situation I search in the online database and I only get names back. Like this:
Name 1
Name 2
Name 3
The situation is now pretty simple. I know you can choice for NSXMLParser but I little bit hate the way the parser works: didStartElement, didEndElement and foundCharacters.
I was wondering if there is a better way to do it. Maybe you have a link to a website with useful information.
There are several. If these are fairly small XML documents (as I expect), then you should look at TouchXML and TouchJSON. If you also want a more full-featured version (including writing XML documents), look at KissXML.