How to parse such kind of Data using NSXML Parsing? - 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

Related

Writing generic XML Parser

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 !

Objective-c Read online xml and store values in an Array data structure

I have to read this link's xml
http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
and parse it to stock in an array with association currency/rate
example:
USD = 1.2948
I know I sould use NSParser but I don't know how create a loop to set array's fields.
Thank you everybody
If you mean NSXMLParser, that is a SAX-style parser, which means you travel the XML tree unidirectionally from beginning to end of file. Every time the parser encounters something significant, it calls one of its delegate methods. In these methods, you read the values and fill your array by adding values one at a time.
It appears awkward at first and the code can become pretty verbose, with lots of conditionals. But SAX parsing is fast and has a small memory footprint.
I strongly recommend studying the examples in Apple's documentation on Event-Driven XML Programming, starting here.
This link will be useful.It is very simple and easy to implement quickly
how to parse xml using libxml2 in iphone
Cheers
// result array is get after use NSXMLPARSER
for (int i=6;i<[resultArray count];i++)
{
[currencyDict setvalue:[[resultArray Objectatindex:i] valueforkey:#"rate"]
forkey:[[resultArray Objectatindex:i] valueforkey:#"currency"]];
}
use this method and the you get currencydict in this format USD = 1.2948 , THB = 39.504 etc
if not understand then post comment to ask question

TTXMLParser Sample Code?

Is anybody famaliar with how to use TTXMLParser. I can't find any documentation or sample code on it.
Is it SAX or DOM?
Does it support Xpath?
Can I extract CDATA from elements?
I have an application that already uses several Three20 modules it would be a shame to have to use another parser.
The main documentation I've found for TTXMLParser is in the header file. The comment there gives an overview of what TTXMLParser does.
TTXMLParser shouldn't really be thought of as an XML parser in the way you are thinking of it -- in this sense, questions such as "is it SAX or DOM" and "does it support XPath" aren't directly applicable. Instead, think of TTXMLParser as a convenience class to take XML and turn it into a tree of Objective-C objects. For example, this XML node:
<myNode attr1="value1" attr2="value2" />
would be turned into an Objective-C NSDictionary node which mapped the key "attr1" to the value "value1" and the key "attr2" to the key "value2".
TTXMLParser internally uses NSXMLParser (which is basically SAX) to build up its tree, but you, as the user of TTXMLParser, don't have to do any SAX-like stuff.
So, no, you will not end up with an XML document on which you can perform XPath queries. Instead, you will end up with an Objective-C tree of objects. If that's what you want, great; if you want a traditional XML parser with XPath, I'm currently working on a project that uses both Three20 and TouchXML. TouchXML supports XPath.
I agree it's hard to find sample code for TTXMLParser. Three20's TTTwitter sample used to use TTXMLParser (well actually, TTURLXMLResponse, which in turn uses TTURLParser), but at some point it was changed to use TTURLJSONResponse instead, which is a shame, because this was their only XML sample.
You can still see the old XML-based sample code here. Specifically, look at the -[requestDidFinishLoad:] function near the bottom of the file, for an example of some code that takes a TTURLXMLResponse, queries its rootObject member, and then walks down the resulting tree of objects.

How can I parse XML using JSON

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/

Quick way to get an NSDictionary from an XML NSData representation?

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/