Iphone parsing large Xml documents - iphone

I just implemented my first XmlParser object (MyParserObj) that relies on the NSXMLParser object.
This parser is embedded inside a tableviewController (MyTableViewController) and it starts parsing at MyTableViewController's viewDidLoad method.
Ok.This is working just fine.It's a small Xml file though! I was wondering if I should choose a different approach when dealing with big Xml files? Will memory suffer when parsing large xml documents?
UPDATE
The real point I want to understand is the flow of the process:
I placed few breakpoints now and it looks like this:
the app first encounters ( inside MyTableViewController's viewDidLoad method )the [MyParserObj parseXMLFileAtURL:path] and starts parsing the Xml document;
The app finishes parsing the whole Xml Document (parserDidEndDocument..);
The tableviewController starts populating its tableView Cells (cellForRowAtIndexPath..);
Apart from choosing a XmlParser (Between those you suggested) that is more or less time/memory consuming are the above steps going to be the same?
If positive, is it correct to think of starting populating the cells as soon as The Parser is done with that specific element?How do I do that?
thanks
Luca

Jim Dovey create a nice blog post about parsing big XML files.
http://blog.alanquatermain.me/2009/04/06/aqxmlparser-equals-equals-big-memory-win/ here Jim describes his one XML parser wich user les memory then other parser.
So if you need to parse large XML files I would suggest you have a look het the open source XML parser AQXMLParser which is the parser create in the blog post.

On iPhone devices, you don't have the NSXMLDocument available on MAC which reads the whole XML document into memory. There are many XML Parser classes available, and you can find them in the link below.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
This tutorial on Ray Wenderlich is what u need to read to know which xml reader is best for your app.
For my own applications, I use SMXMLDocument, you can read about it here:
http://nfarina.com/post/2843708636/a-lightweight-xml-parser-for-ios

for big data, i suggest u json instead of xml, see here a tutorial
http://www.readwriteweb.com/hack/2010/11/json-vs-xml.php

Related

How best to import a lot of text into an iphone app?

I have seemingly a lot of text that i need to get into my iphone app. It's not nearly as much as a book or anything but it would take quite some time to type it all out in xcode, and I'm sure thats not the best way. I read you can import an xml file or maybe a .txt, could someone please point me in the best direction, and maybe a tutorial or something to help me get started?
Thanks!
You can bundle any file you want with your application as a resource. Just include it as part of your project, and then you can load it in your app as an NSString* or NSData*, whichever your prefer/whichever is most appropriate to your data type.
You can also transform the data however your would like, and write some new file that contains the transformed information, and then grab the new file and package that one with the app so that the transformation step no longer needs to be run. For instance, maybe you want to parse your text data and prepopulate a Core Data model with it. You could write the code that does this, grab the .sqlite database file that is generated, and then package the database file in the deployed version of your app so that everyone starts out with an already-populated data model. If that happens to be your use-case here.
What you mean by get text into my iphone app? Do you want to show the text in your application? If yes, why don't you just use the this NSString method:
+ (id)stringWithContentsOfFile:(NSString *)path
encoding:(NSStringEncoding)enc
error:(NSError **)error
If your text needs to be formatted, I would recommend you to use webview to load formatted html files.

Storing data into an XML under iOS

I'm using an NSXMLParser to read data out of a XML, but how can I make my iPhone App storing data into this specific XML.
The case is that I want to create an app to display available dates and one that administrates this dates, whether they are available or not (I'm using an UISwitch to handle this).
Thanks for your answers,
btype.
If you have small amount of data, you can save them in a plist.
See Property List Programming Guide
There's a github project that let's you create XML DOM objects. You can save your XML to your documents folder on the phone.
https://github.com/arashpayan/apxml/

How parse html string in iphone

i m using php file for using data in my application,
in this file i post data on the server and if i get the data from the server
then it is in html formate.
so problem is that i have a string with html tags how i use data in that string.
how i extract data from html string.
Use NSXMLParser class. it works for HTML too. There are three useful delegate methods.
If your HTML out put is some simple data - may be you can write some simple NSString parser your self like 'markhunte' mentioned, if you have large complex data in HTML then you have to go for some open source parsers.
Cocoa does not provide HTML parser, Forum discussion claims in some case XML parser itself work for you, but I never go it working for my data.
In my case I had very simple TAG which I had handled using my own parser using NSString.
I have used the code from --> Flatten-html-content-ie-strip-tags-cocoaobjective-c.
There are also examples of its use on SO.
Just use NSScanner, it is great for searching in between tags that are permanent. If you post some page code I help you set up the scanner.

Parsing source of a webpage with Objective-C

Is there a way to parse a website's source on the iPhone to get the URL's of photos on that page? If so how would you do that?
Thanks
I'd say go for regular expressions - there is a one page library that wraps c regexesthat you can drop into your project.
I recommend regular expressions. There's a great open source Regex library for Cocoa called RegexKit. For the most part, you can just drop it in your code and it'll "just work".
Getting all the urls of images wouldn't be too difficult (less than 20 lines of code) if you assume that all images are going to be in <img> tags. You'd just grab all the image tags (something like: <img\s+[^>]+>), then iterate through those matches. For each match, you'd pull out whatever's in the src attribute: src\s*=\s*("|')?\s*([^\s"']+)(\s|"|')
You might need to tweak that a bit, but it shouldn't be too bad.
There is no super easy way. When I had to do it I wrote a libxml2 SAX parser. libxml2 has an html reader that works fairly well with malformed html, and libxml2 is included with the base system.
You could try it using regular expressions, but I wouldn't recommend that. You should have a look at NSXMLParser, assuming the webpage is coded to be XHTML compliant. TouchXML is another good library.
take a look at Event Driven XML Parsing in the iPhone reference library
Are you OK with any approach you use not picking up on images loaded dynamically via JavaScript.
The closest thing I could see working is to parse out any JavaScript imports, load those up too, and then use a regular expression across the whole file looking for anything that ends in ".jpg/.gif/.png" and grab the full URL out from that. The libxml approach would miss out on references to images not in img tags, but it might well be good enough.

iPhone RSS Reader -- parseXML won't Load some XML feeds

I am using the SIMPLE RSS reading example found at http://theappleblog.com/2008/08/04/tutorial-build-a-simple-rss-reader-for-iphone/
It uses parseXML to load the RSS feeds.
Here is the problem I am having. For the following RSS feed example, I am having trouble getting it to load the feed. Comes up with an error that it cannot connect. However on my Mac RSS Reader it works fine, so I know the link is good.
Any ideas on why it cannot load this particular feed but it can load others fine?
http://www.okstate.com/rss.dbml?db_oem_id=200&media=news
Thanks.
I've just released an open source RSS/Atom Parser for iPhone and hopefully it might be of some use.
I'd love to hear your thoughts on it too!
In my experience, HTML markup causes an RSS parser to fail in most cases. I've experienced a problem like this with a lot of parser classes I've come across (in search of the ultimate one, which I didn't find)
My guess is that entities such as
&#39;s
are responsible for your crash. That was usually the case with my crashes. This also lead to my decision to create a 'proxy server' to pre-parse the XML before sending it to the iPhone (which gives me the advantage of caching, scaling, and some other stuff). I do believe there are solid solutions out there, but is always difficult writing a parser for so many RSS implementations.
P.S: W3C validates this feed as 'valid', so it really is 'our' problem..
Your problem could lie with:
Unicode characters (i.e. I see some o's with two dots above them in the feed)
The code you have doesn't respect CDATA sections correctly
To find out which is the case, save the feed file to your local disk and load it via your code to make sure the error happens.
Do a binary search on the file to find out if a particular RSS entry is causing the problem (i.e. remove all but the first rss entry and see if the problem exists. If it does, then the problem is there, if it doesn't put half the rss entries back in the file and repeat)
I've been experiencing a similar issue. I haven't yet pinned down the answer, but I've noticed that RSS 2 tends to parse more successfully than the rest.
There are many RSS feeds that contain invalid XML, usually because they were hacked together on the server side using HTML templates by somebody who didn't understand XML. I've seen improperly escaped (or non-escaped) HTML post contents, missing close tags, badly nested tags, and so on.
If you want to be able to parse arbitrary feeds, you have to clean up bad XML. The usual way is to use the "htmlTidy" library, which is included in the OS. This can clean up XML as well as HTML.
This example you're following uses NSXMLParser -- I have no idea why. It's a lower-level API and it doesn't support tidying. I would suggest using NSXMLDocument instead. There's a flag in that API that will tell it to use tidy when parsing the XML. This API also returns you the XML as a handy tree of elements that's easy to work with.