I want to create the generic XML parser so that I want to reuse much of code
Use case is I have to parse 10 XML web servers, and all of them have different data So How can i create generic XML Parser so that i use most of the parsing code
Using libxml2 for XML parsing and XPath queries in Cocoa
Use one of the many XML libraries, such as TouchXML or Google's. These will parse documents into object trees, among other things.
Use NSXmlParser I think it helps you !
Related
I am parsing a simple XML file, which one i can use for efficient parsing
The blog writer has compared all the XML parser available for iOS.
you will get the answer of the follwing questions.
1 > XML Parser Performance Comparison App
2 > Which To Choose?
3 > Where To Go From Here?
Check the below blog post
How To Choose The Best XML Parser for Your iPhone Project
There is a blog which compares all parser methods according to their efficiency, you ca find it here.
Sorry for my question. Actually I didnt knew about the JSON, I want to parse the XML file by some new technique. Can anyone help?
In the same way that apples and oranges are both eaten, yet one fruit does not eat the other.
You cant parse XML using JSON, since both are data formats. In other words they cant be used to parse each other, since they are only data "containers", that means that their only purpose is to give data a specific format.
If you want to parse XML, you use a XML parser.
If you want to parse JSON you use a JSON parser.
You may want to change the question wording, but the answer probably is this:
http://code.google.com/p/json-framework/ (also on github https://github.com/stig/json-framework/)
Kickstarter: http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
How should i parse data which contains CDATA in the Attribute?
Read Event-Driven XML Programming Guide
One way to do it is to use NSXMLParser to parse the data. Implement NSXMLParserDelegate's parser:foundCDATA: to capture the data in the CDATA element.
There are lots of examples of how to use NSXMLParser.
If you could, I'ld really suggest converting this to JSON-data. The JSON-framework is lots easier to use.
Else, I'ld suggest to use the TouchXML-classes (but they're a bit complicated in my opinion because they return elements with attributes instead of plain NSDictionaries and NSArrays).
TouchXML: http://code.google.com/p/touchcode/wiki/TouchXML
JSON-framework: http://code.google.com/p/json-framework/
I suggest to read the following Apple documentation, it should explain that.
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/NSXML_Concepts/NSXML.html
I've loaded an XML file as NSData into memory and parse over the elements using NSXMLParser. Although it works, it's a very ugly and hard to maintain code since there are about 150 different elements to parse.
I know there are nice third-party solutions, but I want to keep it with the iPhone SDK for purpose of practice and fun.
So I thought: Why not convert that XML file into an NSDictionary? Having this, I could use fast enumeration to go over the elements.
Or is it just the same amount of ugly code needed to parse and process an XML right away with NSXMLParser?
Would I build up an NSDictionary for every found node in the XML and create a huge one, containing the whole structure and data? Or is there an even simpler way?
NSDictionary cannot read any random xml format. It can only read xml in a specific format which is the plist format.
The plist actually predates xml and the current plist format is just an xml version of the original. The name plist is a contraction of "property list" as the files define the properties of an instance of a class. Therefore, all the xml tags in the file must define elements of an instance of class that implements the NSCoder protocol.
So, yes, if you start with arbitrary xml you must laboriously parse it to convert it into an NSDictionary or some other class.
I wouldn't bother writing a parser from scratch for any reason except as a learning exercise. Every single xml format requires a different parser. It's better to use an existing parser so that 80% of the work is done for you. In a real project, you will end up doing that anyway.
There are many parsers there (e.g. XPathQuery, TouchXML etc.).
Hi dontWatchMyProfile,
You should better user NSString XML format. For this format, I have a little lib converting easily
http://bcaccinolo.wordpress.com/2010/11/14/xml-to-nsdictionary-converter/
I hope it might help.
Cheers,
Benoit
i've not tested this code yet.
http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
in a class i'm writing i'll most likely have to use NSXMLParser twice to parse two different xml's, and i'm wondering which approach should i use?
- release the parser after it finished parsing url and reinitialize when need to parse the second url?
- use different class as delegate for parsing other url?
- or something else?
thanks
peter
In my own personal experience, I've commonly had to parse several different REST xml responses and for each of them I inherit a base class and create one class per request/response/parse. IMHO although this isn't clean code, I honestly find it impossible to write clean code when dealing with a SAX-style parser.
My advice would be separate calls and perhaps separate classes if you don't want a bunch of if-else's in your code. Now if the XML is very similar, it could be a different story...
I have written a class which implements the parser methods and you just have to pass it a string (your url). It returns with an array of elements. It may be of use to you.
You can download it here: http://www.kieranmcgrady.me/helper-classes-for-parsing-xml-files
In the past I've often made classes to parse each response type that I expected to see, you can reuse an NSXMLParser, but I really haven't seen a need to.
Depending on your requirements you may want to just read the responses into nested NSDictionaries, then deal with accessing the elements that you need directly from the dictionaries.