How can I parse XML using JSON - iphone

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/

Related

Can we take data as JSON in optaplanner model rather than XML?

We are using XML files for facts in optaplanner solved examples. Can we use JSON instead of a XML file.
If yes please let me know how we can do it?
You can use whatever you want (including JSON): just replace the XStreamSolutionDao in the examples with your own implementation that read/writes in a JSON format.
As for Java technologies which can do that, that is out of scope for OptaPlanner, but take a look at:
XStream's JSON support
JAXB

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

How to parse such kind of Data using NSXML Parsing?

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

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/