NSXMLParser strange behavior with \n - ios

I'm developing an iOS 4 application that parses a XML file. A piece of XML is:
<?xml version="1.0" encoding="utf-8" ?>
<cards>
<card id ="0">
<name lang="es">The Mad</name>
<description lang="es">...</description>
</card>
...
</cards>
I use the following method to parse <name lang="es">The Mad</name>.
#pragma mark -
#pragma mark NSXMLParserDelegate methods
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
NSLog(#"DidStartElement: %#", elementName);
if ([elementName isEqualToString:#"card"])
{
currentCard = [[Card alloc] init];
NSString* arcaneNumber = [attributeDict objectForKey:#"id"];
currentCard.Number = arcaneNumber;
return;
}
if ([elementName isEqualToString:#"name"])
{
if ([[attributeDict objectForKey:#"lang"] isEqualToString:userLanguage])
{
currentProperty = kNameProperty;
return;
}
}
if ([elementName isEqualToString:#"description"])
{
if ([[attributeDict objectForKey:#"lang"] isEqualToString:userLanguage])
{
currentProperty = kDescriptionProperty;
return;
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!currentStringValue)
{
// currentStringValue is an NSMutableString instance variable
currentStringValue = [[NSMutableString alloc] initWithCapacity:50];
}
[currentStringValue appendString:string];
}
- (void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
NSLog(#"DidEndElement: %#", elementName);
if ([elementName isEqualToString:#"card"])
{
[cards setObject:currentCard forKey:currentCard.Number];
[currentCard release];
currentCard = nil;
return;
}
if (currentProperty == kNameProperty)
{
currentProperty = kNoneProperty;
currentCard.Name = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[currentStringValue release];
currentStringValue = nil;
return;
}
if (currentProperty == kDescriptionProperty)
{
currentProperty = kNoneProperty;
currentCard.Description = currentStringValue;
[currentStringValue release];
currentStringValue = nil;
return;
}
}
After, parsing , I get on currentStringValue the following:
\n\nThe Mad
How can avoid these two '\n'? Why am I getting these two '\n'?
This xml file came from a Windows system, and I've used TextWrangler to covert it to Classic Mac format.

The foundCharacters delegate method also gets called for whitespace in between elements. It will get called if there is say a newline between <cards> and <card id=....
I suggest clearing currentStringValue at the top of didStartElement to discard any characters found before the start of the current element and to make sure only the characters inside the current element (not between) are captured by foundCharacters.
At the top of didStartElement, add:
[currentStringValue release];
currentStringValue = nil;
It is possible for an element's value to contain whitespace (so removing them in didEndElement could result in saving values that don't match the xml content).

On your end element method you can try stripping these out yourself from the current text string. Simply try searching for them on the string and replace them with #"". I like replacing these and unwanted blank entries.

Try this it work for me ,
NSString *data= [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Use above line of code in foundCharacters method .
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
NSString *data= [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if(dString == nil){
dString = [[NSMutableString alloc] initWithString:data];
}else
{
[dString appendString:data];
}
}
Hope this will help for some one .

Related

Web Service - Getting data in Xcode

I'm new to this web service in iOS. I made a sample web service app and made the first stage login successfully. While checking the url in web after login I got this output :
<Rows>
<Row>
<style>LW6001TA</style>
<thumbnail1>
/9j/4AAQSkZJRgABAQEAAAAAAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAHCAV8DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGR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kalDnjy3CPj3Vv6GuVfwD4tRmU+HtQyvXEJP8AKvr+igD4tvtB1fTW232mXluT08yFh/SqXkTZC+VJk9BtNfbrxpIMOisPQjNRfY7YsGNtFuHQ7BkUAfFP2W4HW3l/74NNMEoGTE4+qmvtr7PAf+WMf/fIprWdq4w1tCQexQUAfEm1vQ1IlvPIMxwyOPVVJr7R/sjTf+gfa/8Aflf8Kmis7aBdsVtEi+ioAKAPir7JdAZNtMP+2Z/wqMwTZ/1Un/fJr7cMERH+qT/vkU37Jbf8+8X/AHwKAPiZ7aeNd7wSqvqUIFRV9uyWltNGY5LeJ0PVWQEGsQeA/CgnM3/CPafvJyT5A6/TpQB8eil7V9ReKvhF4c8QWxNnbJpl4v3ZbdAFP+8vQ/zrzCT4DeKFnKpdaeyZ4fzGGR64xQB5Xzmpktrh/uQSsPZCa+lPAnwj0zwygu9UWLUNSP8AEy5jj/3Qep9zXoiWtvGMJBGo9kAoA+LY9K1GVgsdhdOT0Cwsc/pWlH4M8Ty/c0DUT/27t/hX2II0HRFH0FOoA+SrL4Y+M75gI9BuIwf4piIx+pruNB+Ad9Ntk13UY7de8Nt87f8AfR4H6177RQBxmi/Czwhou1otKS4mX/lpdEyH8jx+ldikaRIEjRURRgKowBTqKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/9k=
</thumbnail1>
</Row>
</Rows>
How to get these stuff back in Xcode and view it ? This is the first time I work on this web service I have no idea to get back this response back in Xcode. Please Help !
-(void)parseTheData
{
//Here convert your response to NSData type (NSData *data = [NSData data]; , i have used just for example)
NSData *data = [NSData data];
// Make sure that there is data.
if (data != nil) {
self.xmlParser = [[NSXMLParser alloc] initWithData:data];
self.xmlParser.delegate = self;
// Initialize the mutable string that we'll use during parsing.
self.foundValue = [[NSMutableString alloc] init];
// Start parsing.
[self.xmlParser parse];
}
}
//Delegate methods
-(void)parserDidStartDocument:(NSXMLParser *)parser{
// Initialize the array.
self.arrNeighboursData = [[NSMutableArray alloc] init];
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
// When the parsing has been finished then simply reload the table view or do what ever you want
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(#"%#", [parseError localizedDescription]);
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// If the current element name is equal to "geoname" then initialize the temporary dictionary.
if ([elementName isEqualToString:#"Row"]) {
self.dictTempDataStorage = [[NSMutableDictionary alloc] init];
}
// Keep the current element.
self.currentElement = elementName;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"thumbnail1"]) {
// Store the data like this
[self.dictTempDataStorage setObject:[NSString stringWithString:self.foundValue] forKey:#"thumbnail1"];
}
else if ([elementName isEqualToString:#"style"]){
// Store the data like this
[self.dictTempDataStorage setObject:[NSString stringWithString:self.foundValue] forKey:#"style"];
}
else if ([elementName isEqualToString:#"Row"])
{
//Add the dictionary in array and remove the data from dictionary, so that we can reuse the dictionary
[self.arrNeighboursData addObject:self.dictTempDataStorage];
self.dictTempDataStorage = nil;
}
// Clear the mutable string.
[self.foundValue setString:#""];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Store the found characters if only we're interested in the current element.
if (![string isEqualToString:#"\n"]) {
[self.foundValue appendString:string];
}
}
Here I am just showing you the way to move on.

Parse the Anchor tags and the content using NSXMLParse

I want to parse the html tags inside the "comparison" node
<comparison>
Amazon.com
($34.36) |
Walmart.com
($34.36) |
Rakuten.com
($34.36) |
BestBuy.com
($34.36)
</comparison>
The output I am getting is:
BestBuy.com ($34.36)
The expected output:
Amazon.com ($34.36)
Walmart.com ($34.36)
Rakuten.com ($34.36)
BestBuy.com ($34.36)
But I want to display all the four items.
CODE
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
currentElementValue = [NSMutableString string];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if ([elementName isEqualToString:#"item"]) {
dealsListObj = [[DealsParsingObjects alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"short_title"]) {
dealsListObj.itemTitle = currentElementValue;
currentElementValue = nil;
}
else if ([elementName isEqualToString:#"final_price"]) {
dealsListObj.price = currentElementValue;
currentElementValue = nil;
}
//Detail view
else if ([elementName isEqualToString:#"merchant"]) {
dealsListObj.itemMerchant = currentElementValue;
currentElementValue = nil;
}
else if ([elementName isEqualToString:#"getdeal"]) {
dealsListObj.itemGetDeal = currentElementValue;
currentElementValue = nil;
}
//comparison
else if ([elementName isEqualToString:#"comparison"]) {
dealsListObj.comparison = currentElementValue;
currentElementValue = nil;
}
else if ([elementName isEqualToString:#"item"]) {
[resultArray addObject:dealsListObj];
[dealsListObj release];
dealsListObj = nil;
currentElementValue = nil;
}
}
It appears you have a DealsParsingObjects class with an NSMutableArray called commentsArray. You instantiate that array, when the parsing begins reading the <comparison> element.
But when the parser has read a <comparison> element, you assign the value to a property called comparison; it doesn't get added to the array. Being an default NSString property (assumption on my part), it simply gets reassigned each time the parser is done reading a <comparison> element.
Edit:
parser:didStartElement:... is called every time a new element is read. This also holds for the <a> tags. In that method you reset currentElementValue. So for each <a> the value is basically reset to an empty string. Only the last read value remains, which is the value of the last <a> tag plus the trailing text.

How to parse SOAP xml response in iPhone programming

I am unable to parse soap response,I have tried many ways but not solved my problem,I need the value of USERNAME and PASSWORD.
I am getting following response after soap request :
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<AuthenticateResponse xmlns="http://tempuri.org/">
<AuthenticateResult>
<DocumentElement>
<Status>
<USERNAME>True</USERNAME>
<PASSWORD>true</PASSWORD>
</Status>
</DocumentElement>
</AuthenticateResult>
</AuthenticateResponse>
</soap:Body>
</soap:Envelope>
Here is my code :
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict
{
if ( [elementName isEqualToString:#"AuthenticateResponse"]||[elementName
isEqualToString:#"AuthenticateResult"] || [elementName isEqualToString:#"DocumentElement"])
{
statusArray = [[NSMutableArray alloc] init];
}
else if ([elementName isEqualToString:#"Status"])
{
authenticate = [[Authenticate alloc] init];
}
else if ([elementName isEqualToString:#"USERNAME"])
{
currentElementValue = [[NSMutableString alloc] init];
}
else if ([elementName isEqualToString:#"PASSWORD"])
{
currentElementValue = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentElementValue)
{
[currentElementValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:#"USERNAME"])
{
authenticate.userName = currentElementValue;
}
else if ([elementName isEqualToString:#"PASSWORD"])
{
authenticate.strAuthenticate = currentElementValue;
}
else if ([elementName isEqualToString:#"Status"]) {
[statusArray addObject:authenticate];
}
}
How can i get the values of username and password,can someone help me to parse this.
Thanks in advance.
Try this:
Add 2 properties to your class.
#property (nonatomic, strong) NSString *elementValue;
#property (nonatomic, strong) NSMutableArray *authenticationValues;
Replace your current implementation by:
- (void)parseXML:(NSData *)xmlData {
self.authenticationValues = [NSMutableArray array];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
parser.delegate = self;
[parser parse];
NSLog(#"Authentication values: %#", self.authenticationValues);
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// Keep track of the current element value
self.elementValue = string;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// If the element was 'username' or 'password', add the value to the authenticationValues array
if ([elementName isEqualToString:#"USERNAME"] ||
[elementName isEqualToString:#"PASSWORD"]) {
[self.authenticationValues addObject:self.elementValue];
}
}
To parse the file perform the method parseXML:
When running this you should see the following output on the console:
Authentication values: (
True,
true
)
which are the values for USERNAME and PASSWORD.
Please see the demo prepared for iOS/OSX using sample web service.
It uses swift - SOAP request creator & response parser.
[DEMO][1]
Done using Alamofire & SWXMLHash library
Thanks

Why does this not parse correctly?

Ok so basically, I am trying to parse a sub-element:
<element>
<sub element 1>
<sub element 2>
<sub element 3>
<element>
The current original document that I want to parse contain as below for example:
sub-element-content1: AB, CD, DE, & SOMETHING DISPLAYED.
I only managed to grab & SOMETHING DISPLAYED. I could not parse the whole sub-element.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(#"CURRENT CONTENT: %#", currentNodeContent);
}
That is the 1st issue. The second issue that I am having is that there are supposed to be 11 items in reference to the above. But it only managed to grab 2 items. Please note that each item contains the sub-elements except that some sub-elements are null
Issues has been added: (null)-(null)-(null)
Issues has been added: sub-element-1 sub-element2 sub-element3
Below is portion of didEndElement:
if ([elementName isEqualToString:#"Issue"]) {
//add currentIssue object into issues array
[issues addObject:currentIssue];
NSLog(#"Issues has been added: %#-%#-%#", currentIssue.sub-element-1, currentIssue.sub-element-2, currentIssue.sub-element-3);
currentIssue = nil;
currentNodeContent = nil;
}
if ([elementName isEqualToString:#"sub element 1"]) {
currentIssue.sub-element-1 = currentNodeContent;
NSLog(#"sub element 1: %#", currentIssue.sub-element-1);
}
if ([elementName isEqualToString:#"sub element 2"]) {
currentIssue.sub-element-2 = currentNodeContent;
NSLog(#"sub element 2: %#", currentIssue.sub-element-2);
}
if ([elementName isEqualToString:#"sub element 3"]) {
currentIssue.sub-element-3 = currentNodeContent;
NSLog(#"sub element 3: %#", currentIssue.sub-element-3);
}
Your advice(s) and/or suggestion(s) are greatly appreciated.
Update 1:
Ok, I have solved the problem.
It appears that "currentIssue = nil" causes the object to lose its contents as I was able to print out the currentNodeContent.
So, by removing, currentIssue, I am now able to see all the supposed elements that I need.
But there is one more problem. I can see all the sub elements now, but there only one sub element left which was grabbed partially which I do not understand why.
For <sub element 1> , sub element 1 is not the elementName . The element name is sub.
- (XMLParserViewController *) initXMLParser {
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"Books"]) {
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"])
{
aBook = [[Books alloc] init];
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
if([elementName isEqualToString:#"Book"])
{
[appDelegate.books addObject:aBook];
aBook = nil;
}
else if([elementName isEqualToString:#"name"])
{
aBook.name=currentElementValue;
}
else if([elementName isEqualToString:#"address"])
{
aBook.address=currentElementValue;
}
else if([elementName isEqualToString:#"country"])
{
aBook.country=currentElementValue;
}
currentElementValue = nil;
NSLog(#"%#",aBook.name);
NSLog(#"%#",aBook.address);
NSLog(#"%#",aBook.country);
}
Try this code eith your elements hope this works for you.... :)
Ok, I have solved the problem.
It appears that "currentIssue = nil" causes the object to lose its contents as I was able to print out the currentNodeContent.
So, by removing, currentIssue, I am now able to see all the supposed elements that I need.
Secondly, to make it work correctly, I initialized the array under "parserDidStartDocument" instead of "parserDidStartElement".

How to grab an XML element and store it? Objective C

I am trying to grab a token from a website. Upon successful authentication, an XML document would be displayed.
I created a connection as shown below:
NSString *strURLQueryString = [NSString stringWithFormat:#"%#?username=%#&password=%#", kURL_LOGIN, nameString, passwordString];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strURLQueryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
In order to display the output, I used this:
unsigned char byteBuffer[[receivedData length]];
[receivedData getBytes:byteBuffer];
NSLog(#"Output: %s", (char *)byteBuffer);
So some of the output of the returned document is as shown below:
<status>0</status><reason>User fetched.</reason><token>9cb7396dccabe68c067521db219afb83</token>
I have read many XML parsing implementation but I just could not implement it as it does not fulfil my need, and I just could not understand the complexity of its explanation.
Would appreciate if anyone could give me a good advice on how to go about.
- (XMLParserViewController *) initXMLParser {
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"Books"]) {
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"])
{
aBook = [[Books alloc] init];
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
if([elementName isEqualToString:#"Book"])
{
[appDelegate.books addObject:aBook];
aBook = nil;
}
else if([elementName isEqualToString:#"name"])
{
aBook.name=currentElementValue;
}
else if([elementName isEqualToString:#"address"])
{
aBook.address=currentElementValue;
}
else if([elementName isEqualToString:#"country"])
{
aBook.country=currentElementValue;
}
currentElementValue = nil;
NSLog(#"%#",aBook.name);
NSLog(#"%#",aBook.address);
NSLog(#"%#",aBook.country);
}
Try this ,, I hope this works....
The relevant NSXMLParserDelegate methods:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName //etc
{ _inToken = [elementName isEqualToString:#"token"]; }
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName //etc
{ _inToken = NO; }
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (_inToken) {
_token = string;
[parser abortParsing];
}
}
You should use XML parser.
It is easy to use, go all below steps to get best result:
Create XMLParser class subclasses from NSXMLParser, so you have a NSXMLParser.h and NSXMLParser.m class
your .h class must be like this:
#import <Foundation/Foundation.h>
#interface XMLParser : NSXMLParser <NSXMLParserDelegate>{
NSUInteger parsedCounter;
BOOL accumulatingParsedCharacterData;
BOOL didAbortParsing;
}
#property (nonatomic, strong) NSMutableString *currentParsedCharacterData;
#property (nonatomic, strong) NSMutableArray *currentParsedCharacterArray;
#end
And .m class :
#import "XMLParser.h"
#implementation XMLParser
#pragma mark Parser constants
// Limit the number of parsed data to 100.
static const NSUInteger kMaximumNumberOfFilesToParse = 100;
static NSUInteger const kSizeOfFileBatch = 10;
// Reduce potential parsing errors by using string constants declared in a single place.
static NSString * const kEntryElementName = #"dlResult";
static NSString * const kStringElementName = #"string";
#pragma mark NSXMLParser delegate methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// NSLog(#"didStart");
// If the number of parsed earthquakes is greater than kMaximumNumberOfEarthquakesToParse, abort the parse.
if (parsedCounter >= kMaximumNumberOfFilesToParse) {
// Use the flag didAbortParsing to distinguish between this deliberate stop and other parser errors.
didAbortParsing = YES;
[self abortParsing];
}
if ([elementName isEqualToString:kEntryElementName]) {
_currentParsedCharacterArray = [[NSMutableArray alloc]init];
} else if ([elementName isEqualToString:kStringElementName]) {
accumulatingParsedCharacterData = YES;
_currentParsedCharacterData = [[NSMutableString alloc]init];
}
}
// return string between tags
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// NSLog(#"foundCh");
if (accumulatingParsedCharacterData) {
// If the current element is one whose content we care about, append 'string'
// to the property that holds the content of the current element.
[_currentParsedCharacterData appendString:string];
NSLog(#"currentParsedCharacterData:%#",_currentParsedCharacterData);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// NSLog(#"didEnd");
if ([elementName isEqualToString:kStringElementName]) {
[_currentParsedCharacterArray addObject:_currentParsedCharacterData];
}
accumulatingParsedCharacterData = NO;
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
if (didAbortParsing == NO) {
// Pass the error to the main thread for handling.
[self performSelectorOnMainThread:#selector(handleError:) withObject:parseError waitUntilDone:NO];
}
}
#end
In .m class there are two const string:
static NSString * const kEntryElementName = #"dlResult";
static NSString * const kStringElementName = #"string";
those are string tags,the tags that you should implement are "status", "reason" "token"
from connection class, send data connection to XMLParser class like below:
#autoreleasepool {
// It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
// because it gives less control over the network, particularly in responding to connection errors.
//
XMLParser *parser = [[XMLParser alloc] initWithData:data];
parser.currentParsedCharacterArray = [NSMutableArray array];
parser.currentParsedCharacterData = [NSMutableString string];
[parser setDelegate:parser];
[parser parse];
// depending on the total number of earthquakes parsed, the last batch might not have been a "full" batch, and thus
// not been part of the regular batch transfer. So, we check the count of the array and, if necessary, send it to the main thread.
if ([parser.currentParsedCharacterArray count] > 0) {
// send parsed data to another class or ...
// parser.currentParsedCharacterArray is parsed data
}
parser.currentParsedCharacterArray = nil;
parser.currentParsedCharacterData = nil;
}
if you have any question, ask me!

Resources