How do I read and write XML in Cocoa Touch? - iphone

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!

Related

Converting Objects to plist file then to data

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.

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 create XML editor for iPhone?

I want to create a XML editor (form view type) for iphone. Can anyone suggest me how to proceed in this regard. I know how to parse an XML document but I am having trouble in editing the contents of the file dynamically. This editor should be such that, a new node can be added or a node can be deleted, it can also edit the values.
Please help...
In short you need to parse the document into a readable structure, finding a way to modify that structure, and write it back to XML.
Classes you are likely to need to write include MyXMLDocument, MyXMLNode, MyXMLElement and MyXMLTextNode, and MyXMLNode likely needs NSArray *children;, MyXMLNode *parent; and MyXMLDocument *document; as ivars.
You also probably need something akin to a -stringValue method; and an understanding of how the XML DOM usually works.

What is the use of plist?

In my application, I am using a plist. Please, can anyone explain what are the uses of plist with an example or a sample code?
In the context of iPhone development, Property Lists are a key-value store that your application can use to save and retrieve persistent data.
All iPhone applications have at least one of these by default, the Information Property List:
The information property list is a
file named Info.plist that is included
with every iPhone application project
created by Xcode. It is a property
list whose key-value pairs specify
essential runtime-configuration
information for the application. The
elements of the information property
list are organized in a hierarchy in
which each node is an entity such as
an array, dictionary, string, or other
scalar type.
Plist are XML files in a specific format. Prior to XML, they had a custom format now called 'old plist'. (You almost never see that anymore save in legacy code.)
Foundations collection classes automatically generate XML files in the plist format when you use their serialization methods to write them to disk. They also automatically read them back. You can also write your own serializers for your own custom objects. This allows you to persistently store complex objects in a robust, human readable format.
One use for plist for programmers is that it is easier to use the plist editor to input and manage a lot of data than it is to try and code it. For example, if you have an class that requires setting a large number of ivars, you can create a plist, read it into an NSArray or NSDictionary and then initialize the instance by passing it the dictionary.
I use this technique when I have to use a large number of paths to draw complex objects. You define the path in the plist file instead of the code and edit the path in the plist editor.
It's also a handy way to create a large amount of detailed test data.
PList means PropertyList
It is XML file format
It is mainly user for store and reterve the data
It can store the key-value pair
It's been a long time since I've looked at them, but plist is a short-form of "properties list" and can be used to store application configuration settings that need to persist between instances of an application's execution. Could be similar to a .properties file (I see those a lot on Java projects).
A plist is essentially just a data file, it stores information in a documented format.
From Wikipedia:
In the Mac OS X Cocoa, NeXTSTEP, and
GNUstep programming frameworks,
property list files are files that
store serialized objects. Property
list files use the filename extension
.plist, and thus are often referred to
as plist files. Property list files
are often used to store a user's
settings. They are also used to store
information about bundles and
applications, a task served by the
resource fork in the old Mac OS.
.plist
Info.plist is key/value persistence storage(property list) which is used by system and user. It contains user-friendly text in XML format. Info.plist is mandatory file for any Bundle. For example it contains Bundle id[About] which is usually is used by system but as a programmer/user you are not limited on changing/reading[More]. The same as you can add K/V for your own purposes and read it in runtime. You could noticed that some frameworks forces you to add K/V into your's application to identify you or some other cases.
.entitlements is a property list with enabled capabilities(e.g. ApplePay)
[Info.plist location]
[Vocabulary]

How to read in and parse an XML file on the iphone?

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.