How do I parse this SOAP response in my iPhone application? - iphone

I am getting SOAP response and when parsing the response I am getting this: (As you can see from result which is currently stored as NSString is NSLoged itself contain xml format data)
<Menus>
<Table1>
<Restaurant_Name>CHICK-FIL-A</Restaurant_Name>
<Restaurant_id>2748</Restaurant_id>
<Phone>(404) 371-1466</Phone>
<billing_city>DECATUR</billing_city>
<billing_state>GA</billing_state>
<billing_zip_code>30030</billing_zip_code>
<billing_addr1>105 EAST TRINITY PLACE</billing_addr1>
</Table1>
</Menus>
The Code for the above generated response:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:#"GetMenusByZipCodeResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(recordResults)
{
[soapResults appendString:string];
}
//NSLog(#" soap: %#", soapResults);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
{
if([elementName isEqualToString:#"GetMenusByZipCodeResult"])
{
NSLog(#"soapResult FINAL : %#",soapResults);
recordResults = FALSE;
result.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
The above mention data is result of soapResult.
How to read this data? I tried to reparse it but it is not helping.
Please help me.

If you want a simple XML to NSDictionary converter (including the text nodes), check out http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

Use an XML parser like NSXMLParser
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html

Related

elements in XML parsing

I am new in iphone Development, I don't know much about parsing. But I tried following code to get the distance element text. I am doing XMl parsing Of following link:
http://maps.googleapis.com/maps/api/directions/xml?origin=30.9165904,75.8634752&destination=30.89314000,75.86938000&sensor=true
Please check my code which i have tried:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if([currentElement isEqualToString:#"distance"])
{
NSLog(#"ENTER IN distance");
text = [[NSMutableString alloc]init];
{
dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:text,#"text",nil];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"text"])
{
[dictionary setObject:text forKey:currentElement];
[text appendString:string];
NSLog(#"text....%#",text);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"distance"])
{
[distanparsingarray addObject:dictionary];
}
}
By using above method I am getting the text both of distance and duration. But I want only distance text. Please tell me what I am doing wrong in above code.
thanks in advance.
You have to manage it by below way.
Add on BOOL variable at class level. Set it when you identify currentElement as distance.
Inside found characters append string to text only when above BOOL is true.
Make this BOOL as false in didEndElement when currentElement is not equal to distance.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if([currentElement isEqualToString:#"distance"])
{
flag = TRUE; //created at class level.
NSLog(#"ENTER IN distance");
text = [[NSMutableString alloc]init];
{
dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:text,#"text",nil];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"text"] && flag == TRUE)
{
[dictionary setObject:text forKey:currentElement];
[text appendString:string];
NSLog(#"text....%#",text);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"distance"])
{
[distanparsingarray addObject:dictionary];
flag = FALSE;
}
}

iphone: NSMutable array is empty when using addObject in Xcode 4.3.2

I am parsing data using NSXMLParser.
I am able to get the parsed data. But when I add it to the NSMutableArray using addObject, the array is empty.
I am using the latest Xcode version.
Can someone please help me out.
The array I am working on is identificationTypes1
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"Books"]) {
}
if ([elementName isEqualToString:#"Book"]) {
if ([elementName isEqualToString:#"title"]) {
self.identificationTypes1 =[[NSMutableArray alloc]init];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (!currentElementValue) {
currentElementValue =[[NSMutableString alloc]initWithString:string];
}
else {
[currentElementValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"Books"]) {
NSLog(#"Count is %#",[identificationTypes1 count]);
return;
}
if ([elementName isEqualToString:#"Book"]) {
}
if ([elementName isEqualToString:#"title"]) {
NSLog(#"Curre value is %#",currentElementValue);
[self.identificationTypes1 addObject:currentElementValue];
}
currentElementValue = nil;
}
NSMutabeArray *myArray = [[NSMUtableArray alloc]init];
[myArray addObject:<XML Data>];
You cannot nest elements as you have in didStartElement. Parsers do not allow that.
Take out the title out on its own and retest. You need to make new variables that are accessed conditionally.

How to parse the xml using nsxmlparser with same names of multiple nodes in iphone programming?

How can I parse using NSXMLParser if I need all the images,
<title>Title</title>
<description>my description is here </description>
<images>
<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.7218.jpg</image>
<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.mounted.jpg</image>
<image>http://www.sosmoths.com/mothImages/800px-XN_Tineola_bisselliella_1.jpg</image>
<image>http://www.sosmoths.com/mothImages/Tineola_bisselliella.JPG</image>
</images>
There are more nodes like title, description, so how to parse such xml, I am confused about image nodes, I am thinking to use NSMutableArray for this, but still not clear to do code?
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:"<image>"])
{
[imagearray addObject:imageurl];
}
}
Before this you need to find the element name is and then do above code.This may help you.
use this it work
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
NSLog(#"%#",elementName);
if([elementName isEqualToString:#"moths"]){
mytblarray=[[NSMutableArray alloc] init];
} else if([elementName isEqualToString:#"moth"]){
tmpdic=[[NSMutableDictionary alloc] init];
}
else if([elementName isEqualToString:#"images"]){
imgArray=[[NSMutableArray alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(tmpstr!=nil && [tmpstr retainCount]>0){ [tmpstr release]; tmpstr=nil; }
tmpstr=[[NSString alloc] initWithString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:#"moth"]){
[mytblarray addObject:tmpdic];
[tmpdic release];
}if([elementName isEqualToString:#"images"]){
[tmpdic setValue:imgArray forKey:elementName];
}
else if([elementName isEqualToString:#"image"]){
[imgArray addObject:tmpstr];
}
}
take dictionary in initWithData: methode
Take values as key like title, description
in didStartElement method, in images tag alloc image_array
in didEndElement method, in image tag add your data(here your image link) to image_array
now in didEndElement method, in images tag add that array to your main dictionary as key images
that's all.....

How to parse XML Attributes with XMLParser delegates

getting pretty despirate here.. how on earth do I parse xml that have attributes in them.. I am completely lost,
this is what the xml that I am trying to parse looks like..
<Rows>
<Row SKATERID="706" MANUFACTURER="A-DZG" ISFACT="F" ISSKATE="F"/>
<Row SKATERID="318" MANUFACTURER="A.R.E." ISFACT="F" ISSKATE="T"/>
//...
</Rows>
Using your NSXMLParser Delegate method:
Updated my code. Here is the code which parses your XML.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"Row"])
{
myAttrDict = [[NSDictionary alloc] initWithDictionary:attributeDict];
}
if([elementName isEqualToString:#"Rows"]
{
rowsArray = [[NSMutableArray alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"Row"])
{
[rowsArray addObject:myAttrDict];
[myAttrDict release],myAttrDict = nil;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(#"Rows Array: %#",rowsArray);
}

get attribute values from returned XML

This is the xml I am getting from web:
<?xml version="1.0"?>
<abc>87C4A556-B7E5-5AE4-81FE-86BE0C6306E1</abc>
<abc2>P29077758</abc2>
<abc3>55AGD99D</abc3>
<abc4>147</abc4>
<abc5>1286259226</abc5>
<abc6>USA</abc6>
<abc7>US</abc7>
and using this to get attribute:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:#"a"])
{
recordResults = FALSE;
// greeting.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
Something like that, but I don't have any idea how can I get attribute from returned xml and assign those returned variables into my created variable. How can I do this?
http://blancer.com/tutorials/i-phone/76999/parsing-xml-files/ hope this helps !!!
I highly recommend using TouchXML to do your XML parsing.
I personally had all kinds of issues with NSXMLParser and ended up using TouchXML. Works a treat!
You should implement the following methods:-
One way you can use is :-
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
//check that the element is the one you want. If it is then initialize the string in which you want to store the value.
if(elementName isEqualToString:#"abc")
{
tempString = [[NSMutableString alloc] init];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[tempString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if(elementName isEqualToString:#"abc")
{
self.StringToStore = tempString;
}
}