Xml Parsing on iPhone - iphone

I have checked lots of tutorials on xml parsing in iOS ..
but most of them have already set the xml response from the server in a clear understandable line by line format
<?xml version="1.0" encoding="utf-8"?>
<people> <person>
<firstName>James</firstName> <lastName>Franklin</lastName>
</person>
<person>
<firstName>Snidely</firstName>
<lastName>Wilsonn</lastName>
</person>
<person>
<firstName>Dudley</firstName>
<lastName>Doright</lastName>
</person> </people>
Now seeing this.. it is pretty easy to follow what tags you have to filter and extract data from ..
But now when i am doing this . (like this in my app)
NSError *error;
NSData *linkData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"someURL"] options:NSDataReadingUncached error:&error]
NSString *stringData = [[NSString alloc]initWithData:linkData encoding:NSUTF8StringEncoding];
NSLog(#" Data /n/n/n %#",stringData);
It is coming like this in console :
Data /n/n/n {"entries":[{"id":"37504814","title":"Live Blog: Patrik Schumacher","created":"2012-02-09 23:43:00","updated":"2012-02-10 03:34:05","user_id":"15084","entry_type":"20","location":null,"portfolio_id":null,"arc_name":"Lian Chikako Chang","parent_image":"xyz2sb7y9n4mtvbb.jpg","link":"http:live-blog-patrik-schumacher","quote":null,"content":" \n<p>\n\tHi Archinect!<\/p>\n<p>\n\tPatrik Schumacher, partner at Zaha Hadid Architects and founding director at the AA Design Research Lab, is in Piper tonight for a lecture on \"Parametric Order: 21st century architectural order.\"<\/p>\n<p>\n\t<img alt=\"\" src=\"http
NOw this is JSON and XML both .. but there are thousands of lines more after it /...and i am not able to understand it as it is...any way to get it in the proper formatted format like the above given XML.??
ALSO one more question : Is the XML parser best for parsing or should i do it with other projects for xml parsing on github?.
Thanks

this will format xml the xml for you to make it more readable.
http://xmltoolbox.appspot.com/

What you get is JSON string with HTML tags embedded. (Is it intended?. or the response is corrupt).
You can't parse the HTML with XML Parser.
NSXMLParser is a SAX parser. It is good for large amount of data.

I am unable to understand your question. But you need to take look at following tutorials. You really need it :
How To Choose the Best XML Parser for your iPhone Project
How to Read and Write XML Documents with GDataXML
How to Translate Text with Google Translate and JSON on the iPhone
It will really help you.

Your response content is in JSON format.And in turn it contains HTML data for the key : **content**.
NSXmlParser is both DOM and SAX parser. But the selection of the parser depends upon various factors. You can refer to this blog for how to select a XML Parser.

Related

objective c string encoding issue

My app receives some data from server which is in XML format and some tags contains Japanese words. When I tried to execute the link in the browser, it gets the correct response in Japanese. For example:
<root>
<categorylist>
<cid>19</cid>
<cname>ファミリー</cname>
<lang>jap</lang>
</categorylist>
</root>
..but in my app, the response string is in some encoded format like the following:
<root>
<categorylist>
<cid>19</cid>
<cname>ファミリー</cname>
<lang>jap</lang>
</categorylist>
</root>
My code to receive XML from the server is:
NSString *req=#"http://www.myserver.com/category.php?lang=jap";
NSString *resultString=[NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:nil];
Is there a problem with my code? How can I convert the response to correct Japanese strings?
Try looking at some of the decoding methods listed on this question: HTML character decoding in Objective-C / Cocoa Touch

Converting String values to xml

I have some string values that are retrieved from json data. I need a process of converting this strings to xml format and Post it to a url
If it's inside iPhone SDK you will have to parse the string to convert it to XML, unless you can just post the xml as-is.
Anyway. To parse the XML, you have basically two easy options:
NSXMLParser
libxml2 (my preferred choice, don't know why)
Then you can use NSURLConnection class to post the information to the desired XML. There are a lot of Stackoverflow questions already covering this issue you can search and use.
You can also look at an example here.

how to parse self closing tag in xml file

I am working on an iphone application in which I am consuming a webservice.
So i am parsing the XML file data. any idea about how to parse self closing tag
like: State/ and how to read data of self tag like: Contact Email="abc#anc.com" Name="PhD" Phone="123-521-3388" Source="location"/
I am parsing xml file using NSXMLPARSER class methods and library
Thanks,
Your sample XML data
<Contact Email="abc#anc.com" Name="PhD" Phone="123-521-3388" Source="location"/>
is equivalent to
<Contact Email="abc#anc.com" Name="PhD" Phone="123-521-3388" Source="location"></Contact>
and should be parsed by any XML parser.

Get NSString/NSData object from hardcoded bytes saved as NSString object in webservice response

I am working with the iPhone SDK and I have to process a webservice response I receive from an external service.
The response data consists of an XML string that was UTF8-encoded to a byte array.
This byte array is converted to string
This string is put into a XML wrapper element
The wrapper is returned via an HTTP response
Therefore I need to know how to convert the response data back to the XML string it used to be. Unfortunately, I cannot change the way my response is created, so I have to deal with it somehow.
Example of the raw data I get from the webservice:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body><ExportBytesResponse xmlns="http://knowledgepark-ag.com/webservices/">
<ExportBytesResult>PERhdGE+PFJlY29yZCBEQj0iOTk5OTQiIEtleT0iMjA1Mjc0Ij48U2luZ2xlIEl0ZW1JZD0iQVJCUEFLRVQiIFZhbHVlPSIyOTAiIC8+PFNpbmdsZSBJdGVtSWQ9IkhET0tOUiIgVmFsdWU9IjIwNTI3NCIgLz48U2luZ2xlIEl0ZW1JZD0iVkVSQSIgVmFsdWU9IjEuNzEuMzIxMy4xNzU3NyIgLz48U2luZ2xlIEl0ZW1JZD0iRElTIiBWYWx1ZT0iR2VNLCBNw6RyIDEwIDIwMDggIDU6NDFQTTomI3hEO1ouenQuIGV4aXNpdGVydCBrZWluZSBNw7ZnbGljaGtlaXQgZGFzIEJlZW5kZW4gZWluZXMgTW9kdWxzIGR1cmNoIEFiYnJlY2hlbiBvZGVyIEhhcmQtQ2xvc2UtWCB6dSBlcmZhaHJlbi4gQW4gZGllc2VyIFN0ZWxsZSBtw7xzc3RlbiBtZWlzdCBVc2VydmFycyBkZXMgTW9kdWxzIGdlbMO2c2NodCB3ZXJkZW4uIE9kZXIgYW5kZXJlIEF1ZnLDpHVtYWt0aW9uIGR1cmNoZ2Vmw7xocnQgd2VyZGVuLiYjeEE7RUxLRUcgMDYuMDguMjAwOCAwOToyMToxNyYjeEE7JiN4QTsgQmVpc3BpZWxhdWZiYXUgOiYjeEQ7JiN4QTsmbHQ7Q2xv</ExportBytesResult>
</ExportBytesResponse></s:Body>
How do I get back my old xml string representation hidden inside these raw bytes?
Any help is highly appreciated, I feel just stupid right now for not being able to come up with a solution.
Best Regards,
David
ExportBytesResponse is Base64 encoded, so you must first decode that node using something like this.
That will give you an NSString containing the XML. Then you can use NSXMLParser to parse your data.

NSXMLParser init with XML in NSString format

I just ran into this one and couldn't seem to get any clear answer from the documentation.
Im retrieving some XML through a HTTPS connection. I do all sorts of authentication etc. so I have a set of classes that deals with this in a nice threaded way.
The result is an NSString that goes something like:
<response>
//some XML formatted test
</response>
This means that there is no encoding="UTF-8" indent="yes" method="xml" or other header blocks to indicate that this is actual XML and not just an NSString.
I guess I will use [NSXMLParser initWithData:NSData] to construct the parser, but how will I format or cast my NSString of xml formatted text into a proper NSData object that NSXMLParser will understand and parse?
Hope it makes sense, thank you for any help given :)
You can convert a string to a NSData object using the dataUsingEncoding method:
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
You can then feed this to NSXMLParser.
The headers are optional, but you can insert the appropriate headers yourself if needed:
NSString *header = #"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
NSString *xml = [NSString stringWithFormat:#"%#\n%#", header, response);
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *praser = [NSXMLParser initWithData:data];
By the time NSXMLParser has the data it's pretty much going to be expecting it to be XML ;-)
I'm pretty sure the processing instruction header is optional in this context. The way you get the NSString into the NSData is going to dictate the encoding (using dataUsingEncoding:).
(edit: I was looking for the encoding enum, but Philippe Leybaert beat me to it, but to repeat it here anyway, something like: [nsString dataUsingEncoding:NSUTF8StringEncoding])
I've passed NSStrings of XML in this way before with no issues.
Not specific to this question as such, but on the subject of XML parsing in an iPhone context in general you may find this blog entry of mine interesting, too.