I am facing a problem in parsing data from a web service response.
I call a web service and get the response as NSLog, but I need to capture the data as NSString.
Here is my sample code:
-(void)connection:(NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response {
}
-(void)connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
NSString *strData;
strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];
soapResultsString=[[NSMutableString alloc]init];
recordResults =YES;
}
NSString *xmlparserString;
NSMutableString *soapResultsString;
bool recordResults;
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict
{
xmlparserString=elementName;
if( [xmlparserString isEqualToString:#"ns:return"])
{
recordResults =YES;
soapResultsString = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if( recordResults )
{
[soapResultsString appendString: string];
NSLog(#"inside%#",string);
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if( [elementName isEqualToString:#"ns:return"])
{
NSLog(#"parser==>%#",parser);
NSLog(#"nameSpaceUrL==>%#",namespaceURI);
NSLog(#"qName==>%#",qName);
NSData *data = [soapResultsString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(#"json==>%#",json);
NSArray *planArray = [json objectForKey:#"hello_History"];
NSLog(#"planArrayCount==>%lu",(unsigned long)[planArray count]);
NSMutableArray *dataSource = [NSMutableArray arrayWithCapacity:planArray.count];
for (int i = 0 ; i<[planArray count]; i++)
{
recordResults = NO;
txn_id=[[[json valueForKey:#"hello_History"]valueForKey:#"date"]objectAtIndex:i];
loginId = [[[json valueForKey:#"hello_History"]valueForKey:#"time"]objectAtIndex:i];
}
}
}
the problem is i getting outputupto this method
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
but my parsing method not at called
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
is there any alternate way to capture the string
Related
I have a NSXML parser that is willing to retrieve data out and put it into NSMutable array. It works when i NSLog the array out. But when i display the array into the table view it fails. Is good if can tell me more specific on which error that i have because there is no error when running.
This is my ViewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// output = [[NSMutableArray alloc]initWithObjects:#"red",#"green", nil];
NSLog(#"Response recieved");
output= [[NSMutableArray alloc] init];
feeds = [[NSMutableArray alloc] init];
// NSString *severity = #"Informational";
NSString *soapMessage = #"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
" <IncidentGetList xmlns=\"https://www.monitoredsecurity.com/\">"
"<Severity></Severity>"
"<SourceOrganization></SourceOrganization>"
"<DestinationOrganization></DestinationOrganization>"
"<MaxIncidents></MaxIncidents>"
"<SourceIP></SourceIP>"
"<Category></Category>"
"<ExcludeCategory></ExcludeCategory>"
"<StartTimeStampGMT></StartTimeStampGMT>"
"<EndTimeStampGMT></EndTimeStampGMT>"
"<CustomerSeverity></CustomerSeverity>"
"</IncidentGetList>"
"</soap:Body>"
"</soap:Envelope>";
NSURL *url = [NSURL URLWithString:#"https://api.monitoredsecurity.com/SWS/incidents.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"https://www.monitoredsecurity.com/IncidentGetList" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[connection start];
if(connection)
{
webResponseData = [NSMutableData data] ;
}
else
{
NSLog(#"Connection is NULL");
}
}
This is my NSXMLparser
//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:#"SecurityIncidentSummary"]) {
IncidentGetList = [[NSMutableDictionary alloc] init];
Severity = [[NSMutableString alloc] init];
} //NSLog(#"current element: ", elementName);
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(#"File found and parsing started");
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([element isEqualToString:#"Severity"]) {
[Severity appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"SecurityIncidentSummary"]) {
[IncidentGetList setObject:Severity forKey:#"Severity"];
[feeds addObject:[IncidentGetList copy]];
}
[self.datalist reloadData];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"Final Feed : %#",feeds);
//[self.datalist reloadData];
}
This is my tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
return [feeds count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(cell == nil){
cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text= [[feeds objectAtIndex:indexPath.row]objectForKey:#"Severity"];
//cell.textLabel.text= output[indexPath.row];
return cell;
}
Check out this XMLParser example which I have done.
In your .h file:
#interface RssViewController : UIViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
{
NSString *element;
NSXMLParser *xml;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSMutableString *description;
NSMutableArray *arrmute;
}
#property (weak, nonatomic) IBOutlet UITableView *tview;
In .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
arrmute=[[NSMutableArray alloc]init];
NSURL *url=[[NSURL alloc]initWithString:#"http://news.google.co.in/news?cf=all&hl=en&pz=1&ned=in&topic=tc&output=rss"];
xml=[[NSXMLParser alloc]initWithContentsOfURL:url];
xml.delegate=self;
xml.shouldResolveExternalEntities=YES;
[xml parse];
}
NSXMLParser delegates methods :-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:#"item"])
{
item=[[NSMutableDictionary alloc] init];
title=[[NSMutableString alloc] init];
link=[[NSMutableString alloc] init];
description=[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([element isEqualToString:#"title"])
{
[title appendString:string];
}
else if ([element isEqualToString:#"link"])
{
[link appendString:string];
}
else if ([element isEqualToString:#"description"])
{
[description appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"item"]) {
[item setObject:title forKey:#"title"];
[item setObject:link forKey:#"link"];
[item setObject:description forKey:#"description"];
[arrmute addObject:[item copy]];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self.tview reloadData];
}
TableView Methods:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrmute.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[[arrmute objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
Hope this will help you...
I need to get XML data from this particular address (https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml). But when I run the application, nothing happens. Also honestly I do not know how to get from the above xml id 1, id 2 and so on. I'll be happy for any of your advice. Thx
- (id)initWithArray: (NSMutableArray *)slovoArray {
self = [super init];
if (self) {
self.slovoArray = slovoArray;
}
return self;
}
- (void)parseXMLFile
{
NSURL *url = [[NSURL alloc] initWithString:#"https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml"];
self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
self.parser.delegate = self;
[self.parser parse];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([_element isEqualToString:#"radek"]) {
_item = [[NSMutableDictionary alloc] init];
self.kod = [[NSMutableString alloc] init];
self.kurz = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([self.element isEqualToString:#"kod"])
{
[self.kod appendString:string];
}
else if ([self.element isEqualToString:#"kurz"])
{
[self.kurz appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"radek"]) {
Slova *thisSlovo = [[Slova alloc] initWithName:self.kod
kurz:self.kurz];
[self.slovoArray addObject:thisSlovo];
}
self.element = nil;
}
#end
Try this
NSURL *url = [NSURL URLWithString: #"Enter Here your webservice url" ];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * rsltStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:rsltStr error:&parseError]; // In this I have used XMLReader file
Download XML Reader file from here : - XML Reader Download
For more help about XMLReader Visit here :- SOAP webservice calling in iOS with xml parsing
To read XML data from URLs, I recently wrote a simple XML parser for iOS called ConiferXML that might do what you're looking for. You can check it out at GitHub. If this doesn't work out there is also another library on GitHub that is a little more complex but does the same thing.
Iam new to ios development, i need to parse xml data into an array, i tried a tutorial, and i am not getting data, when edited with "hrms.atrity.info/api/people"
XML url ,Parser delegates not working while executing with the above link.Here is my coding, it works with "image.apple.com/main/rss/hotnews/hotnews.rss".How to rectify this probe,
- (void)viewDidLoad
{
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://hrms.atrity.info/api/people"];//NOt Working
NSURL *url = [NSURL
URLWithString:#"http://image.apple.com/main/rss/hotnews/hotnews.rss"];//This Works
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"
forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: #"FirstName"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:#"Users"])
{
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"Users"])
{
[item setObject:title forKey:#"FirstName"];
[item setObject:link forKey:#"LastName"];
[feeds addObject:[item copy]];
NSLog(#"%#",feeds);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([element isEqualToString:#"FirstName"])
{
[title appendString:string];
}
else if ([element isEqualToString:#"LastName"])
{
[link appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self.tableView reloadData];
}
Thanks in Advance.
Using the following code:
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://hrms.atrity.info/api/people"]];
is resulting in the parser:parseErrorOccurred: being called for the http://hrms.atrity.info/api/people URL. The NSError passed to that delegate shows an error code of 4 which translate to NSXMLParserEmptyDocumentError. This is because the URL is returning JSON instead of XML. You can check this using the following code:
NSError *error;
NSString *data = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://hrms.atrity.info/api/people"] encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", data);
You can clearly see in the console in Xcode that the data is JSON. In order to retrieve the data in XML format, you need to specify the Accept header with a value of application/xml. You cannot do this with NSURL alone and will need to use NSMutableURLRequest so that you can set the custom header value, as follows:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://hrms.atrity.info/api/people"]];
[request setValue:#"application/xml" forHTTPHeaderField:#"Accept"];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
xmlParser = [[NSXMLParser alloc] initWithData:data];
I have an outdated application which use to download an XML document and parse it on the iPhone app, I used the NSURLConnection for that purpose:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(#"Response :%#",response);
responseData = [[NSMutableString alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *str = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
[responseData appendString:str];
[str release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DATA : %#",responseData);
if (responseData != nil) {
[self startParsing:responseData];//Parse the data
[responseData release];
}
}
Since moving to use NSXMLParserDelegate with AFXMLRequestOperation, I cannot figure out a way to get xml data properly:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[responseData appendString:elementName];
[responseData appendString:namespaceURI];
[responseData appendString:qName];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[responseData appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
[responseData appendString:elementName];
[responseData appendString:namespaceURI];
[responseData appendString:qName];
}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
[SVProgressHUD showSuccessWithStatus:#"Downloading completed"];
NSLog(#"DATA : %#",responseData);//not properly appended, tags delimeters are missing
if (responseData != nil) {
[self startParsing:responseData];
[responseData release];
}
}
How to append all the data received from the server in the responseData mutable string ? I debugged the data received after finishing downloading and the xml is missing tags delimeters <>. I think I ma missing the way to get the xml data.
P.S: Please note it's important that I get the xml in a NSMutableString object.
#Fermi
I used AFURLConnectionOperation as you recommended, it works fine with my purpose, but I noticed that my received data is not catched by the delegate methods, instead I can get the data in a completion block:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:API_URL]];
AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
operation.completionBlock = ^{
NSLog(#"Complete: %#",operation.responseString);//responseString is my data
};
[operation start];
[SVProgressHUD showWithStatus:#"Downloading files"];
wo since NSURLConnection delegate methods are not called, how can I manage failure, etc? Thanx.
AFXMLRequestOperation is explicitly intended to be used to return you an NSXMLDocument instance, NOT a raw XML string.
If you want the XML string use AFURLConnectionOperation and build the NSMutableString the same way you do with NSURLConnection.
I use a webservice to take information. When I take string result, I can not write it into an array.
In foundCharacters when I write NSLog(#"%#",string); it writes all values correctly but when I write:
[myArray addObject:string];
NSLog(#"%#",myArray);
I see lots of nulls. How can I take these values and write them into an array :(
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[datawebservice setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[datawebservice appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
xmlparserr = [[NSXMLParser alloc] initWithData:datawebservice];
[xmlparserr setDelegate:self];
[xmlparserr parse];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
elementyedek = elementName;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([elementyedek isEqualToString:#"StokAdi"]) {
[myArray addObject:string];
NSLog(#"%#",myArray);
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConnection");
}