I want to create a xml file dynamically for my Iphone application. I tried it by using NSXMLDocument but NSXMLDocument is not declared in iphone application.
Please help me in doing so.
Thanks
Gaurav
Try the open source XML stream writer for iOS:
Written in Objective-C, a single .h. and .m file
One #protocol for namespaces support and one for without
Example:
// allocate serializer
XMLWriter* xmlWriter = [[XMLWriter alloc]init];
// start writing XML elements
[xmlWriter writeStartElement:#"Root"];
[xmlWriter writeCharacters:#"Text content for root element"];
[xmlWriter writeEndElement];
// get the resulting XML string
NSString* xml = [xmlWriter toString];
This produces the following XML string:
<Root>Text content for root element</Root>
You can use KissXML to generate XML files on the iPhone.
You can also use libxml2 to generate XML data on the iPhone.
Related
I'm creating a simple iOS application consisting of a few UITableViewControllers. The information displayed in the view controllers will come from a xml file (that I'll include in the project's Resources or direct from dropbox or iCloud). The xml file's contents will be based on user input .
A few notes:
The data is based on the user input means not static. Ideally the app will load the data into "Core Data" from xml file.
Each additional run of the app will just pull data from some Core Data source (that I'm not completely familiar w/ yet) instead of re-loading it from the textfile.
right now I am using XMLwriter to generate simple xml file
Please guide me
thank you
The best pattern here seems to be to use the XML file to "seed" your Core Data database. This only happens the first time. After that you will never again use your XML file but simply update and sync your core data store.
This is far better than generating XML. The problem with XML files (like property lists) is that you have to write the entire file for each little incremental change. If you sync to a store somewhere online, this can take much too much time to be practical.
Assuming you can get a foundation object from the XML file, simply iterate through the object and insert a Core Data one by one.
for (NSDictionary *dict in xmlArray) {
Entity *newObject = [NSEntityDescription
insertNewObjectForEntityForName:#"Entity"
inManagedObjectContext:self.managedObjectContext];
newObject.attribute1 = [dict objectForKey:#"attribute1"];
newObject.attribute2 = [dict objectForKey:#"attribute2"];
// etc...
}
[self.managedObjectContext save:nil];
My application contains 8 TextFields. When I click the submit button I want to save all the values from the TextFields into an XML file (and generate a XML file if required). How can I do this?
Another XML writer which will get the job done is XSWI (shameless plug - I wrote that code).
There are many ways you could do this. One quick and dirty way of doing it is to generate a string in your code and insert the values
e.g.
NSString *myXML = [NSString stringWithFormat:#"<myxml><value1>%#</value1><value2>%#</value2></myxml>", textfield1, textfield2];
Then open a file and write myXML to it.
Not very elegant, but it depends on what exactly you want.
Do you need the XML file to be in a specific format? If not the easiest way is to save it as a plist (a type of xml file) by putting your strings into an array and then saving the array as a plist using
NSArray *array = [NSArray arrayWithObjects:label1.text, label2.text, label3.text, etc, nil];
[array writeToFile:fileName atomically:YES];
The nice thing about the plist is that you can easily load the string again by calling:
NSArray *array = [NSArray arrayWithContentsOfFile:filename];
If you need your XML in a different format from what the plist produces, the easiest way is probably just to build the XML yourself using string concatenation, e.g.
NSString *xml = [NSSString stringWithFormat:#"<root><label>%#</label><label>%#</label><label>%#</label>etc</root>", label1.text, label2.text, label3.text, etc];
You can use GDATAXML to read and write XML to file.
Refer this link to see how its done.
http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml
Obj-C or Monotouch.Net C# answers are fine.
I have a Base64 string that is a PDF document received over a web service. I can get the NSData.
How do I take the NSData and save it as a PDF?
-- I get the NSData this way --
byte[] encodedDataAsBytes = System.Convert.FromBase64String (myBase64String);
string decoded = System.Text.Encoding.Unicode.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
The simplest way to save it is probably to use NSData's writeToFile:options:error: method.
I found that using the .NET framework works better than trying to use the iOS framework for this problem. This will take any file and convert it to it's original then save it to the iPhone/iPad device. "path" is just a folder on the dev ice.
using (var f = System.IO.File.Create (path))
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String (Base64PDFString);
f.Write (encodedDataAsBytes, 0, encodedDataAsBytes.Length);
}
I'm working on a project where I recently had to accomplish the same thing you are describing. I get base64 encoded PDF files as strings from a .NET web service which need to be decoded to their original and saved as PDF files in the applications documents directory.
My solution was:
Use ASIHTTPRequest to communicate with the web service.
I then use TBXML to parse incoming xml and get the base64 as an NSString.
To decode the string I use a method from QSUtilities library called decodeBase64WithString.
Finally I save the result with NSData's writeToFile.
I have tested and successfully used this method with PDF files that are up to 25mb. I also had a couple of test runs with a 48mb file but that file made the decodeBase64WithString method take up too much memory and my app crashed. Haven't found a solution to this yet..
If you are working with multiple large files be sure to free up your memory once in a while. I got all my files in one loop in which I had to use my own nsautorelease pool and drain it at the end of the loop to free up any autoreleased objects.
i'm new as an iphone developer, as a hobby i'm writting a small game (just for fun).
i would like to store the default configuration inside a xml file. I have the xml file, I know about how to parse the xml document, but.. what i do not know, and that's why i'm here is:
is there a way to say to Xcode: imagine this (xml) file is stored on /documents/ ? or the unique way is to copy&paste the (xml) file here:
~/Library/Application Support/iPhone Simulator/4.1/Applications//documents/
thanks!
fyi: the xml will always be together with the app.
just drag the xml into the file tree (solution explorer) in xcode
then to get the root directory path, just call
[[[NSBundle mainBundle] bundlePath] UTF8String]
that gets it as a char*, for c style string stuff. otherwise just leave out the UTF8String
[[NSBundle mainBundle] bundlePath] to get the NSString. just add your file name to the end
in my experience, it doesn't matter where you dump your xml in the 'solution explorer' it always ends up in the root directory.
better to use plist instead of xml . if u use plist then there is no need to parse it again.
end then use
NSString *theFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:#"documents/YOUR FILE NAME"]
What's the best way to create a binary plist on the iPhone with simple string based key/value pairs? I need to create a plist with a list of recipe and ingredients. I then want to be able to read this into an NSDictionary so I can do something like
NSString *ingredients = [recipes objectForKey:#"apple pie"];
I'm reading in an XML data file through an HTTP request and want to parse all of the key value pairs into the plist. The XML might look something like:
<recipes>
<recipe>
<name>apple pie</name>
<ingredients>apples and pie</ingredients>
</recipe>
<recipe>
<name>cereal</name>
<ingredients>milk and some other ingredients</ingredients>
</recipe>
</recipes>
Ideally, I'll be able to write this to a plist at runtime, and then be able to read it and turn it into an NSDictionary later at runtime as well.
Creating a Property List in Objective-C
This contains information for creating a property list file using either CoreFoundation or Cocoa.
For your specific case, look at the interface for NSDictionary and use:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;
If you only need one fairly small dictionary, you could also try NSUserDefaults.
- (void)setObject:(id)value forKey:(NSString *)defaultName;
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;