storing integer in a int variable using NSXMLParser, in obj c - iphone

hii every one
i am doing a iphone project in that i need to take a int value from xml so
how can i store a integer value which is stored in a xml, into a int variable using NSXMLParser (by parsing the xml)
can any one give me some examples,,, thanx in advance

Here is one way (assuming you already know how to use NSXMLParser)
xml:
<intVariable>10</intVariable>
code:
- (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 *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"intVariable"])
intVariable = [currentElementValue intValue];
[currentElementValue release];
currentElementValue = nil;
}
(currentElementValue and intVariable are mutable string and int members respectively).

Related

How to parse the xml file have same tag name using nsxmlparser?

How do I parse this type of XML using NSXMLParser
<category> <categoryName> userName</categoryName> <categoryName>password</categoryName><category>
Declare array and a string in h file like:
NSMutableArray *aryCategory;
NSString *strCategroyName;
in .m file for starting Parser use:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:yourData]; // your data will be instance of NSData or NSMutableData
parser.delegate = self;
[parser parse];
this will be done once you get your xml data. For handling result you can use NSXMLParserDelegate as follows:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"category"]) {
aryCategory = [[NSMutableArray alloc] init];
strCategroyName = #"";
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
strCategroyName = [NSString stringWithFormat:#"%#%#", strCategroyName, string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"categoryName"]) {
[aryCategory addObject:strCategroyName];
strCategroyName = #"";
}
}
Now you will have your array fill with all of your category name.
Hope this helps :)
Write the <category> in didStart and didEnd elements which are the parser's delegates.

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 xml for ios development

So I know how to parse some XML structures but I am currently trying to parse this specific xml structure thats a bit different to what I am used to.
normally I would parse something like
<xml>
<data>
<name>Forrest</name>
<age>25</name>
<username>forrestgrant</username>
</data>
</xml>
But now I'm working with some xml like so..
<xml>
<data name="Forrest" age="25" username="forrestgrant" />
<other value="6" />
</xml>
how do I access those variables when they are structured like this?
This is how I would normally approach this task, which is baised of searching for the title tags and getting the data from each one.. however I am now trying to figure out how to parse this other style of xml.
- (void)startTheParsingProcess:(NSData *)parserData
{
[myDataArray release]; // clears array for next time it is used.
myDataArray = [[NSMutableArray alloc] init]; //initalizes the array
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //incoming parserDatapassed to NSXMLParser delegate which starts parsing process
[parser setDelegate:self];
[parser parse]; //Starts the event-driven parsing operation.
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:#"item"]) {
// NSLog(#"Found title!");
itemString = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[itemString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqual:#"item"]) {
//NSLog(#"ended title: %#", itemString);
[myDataArray addObject:itemString]; //this is where i pass the values over to my array.
[itemString release];
itemString = nil;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
// Passes myDataArray to the method that will sort and display the array into a uitableview.
[self startSortingTheArray:myDataArray];
}
any help would be greatly appreciated..
The xml data above provides the data as attributes on the xml element.
This callback method gives you access to the attributes as a dictionary of key values (attributeDict).
(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
NSLog the dictionary out in that method to see the values:
NSLog(#"attributes: %#", attributeDict);
In your didStartItem: method, the attributes dictionary will contain values for all the XML attributes.

Using dictionaries with XML (iphone/iOs)

I want to parse data from a xml file. This is working so far, but I don't find the correct way to get data from the dictionary.
My parser looks as follows
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"Placemark"]) {
placemarkData = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:#"name"] || [currentElement isEqualToString:#"address"] || [currentElement isEqualToString:#"coordinates"])
[currentTitle appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"Placemark"]) {
[placemarkData setObject:currentTitle forKey:currentElement];
[Placemarks addObject:[placemarkData copy]];
NSLog(#"adding story: %#", currentElement);
}
As you can see. The data is stored in a dictionary, and this dictionary is store in an array. Only problem is... the key for dictionary is always coordinates. This look kinda logical to me... but I don't find the logic to implement it the right way. Can somebody have a look?
Thnx in advance!
When you fill a dictionary, you set objects for keys. To get an object from a dictionay, you need to have the key. And I think that is your problem.
To list the keys in your dictionary, you can use a keyEnumerator:
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
/* code that uses the returned key */
NSString *theElement = key;
NSLog(#"Element: %#", theElement);
}
With this code, you will list all the keys. To find the object with the key, you use [myDictionary objectForKey:key];. If you know the elements in your XML you can access them with this line.
I think the problem you are facing is because of the logic you used in
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"Placemark"]) {
placemarkData = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
}
}
method, i.e. allocating the placemarkData that will work fine if element Placemark is encountered first time but for the next iteration there will be problem, follow this idea and see the response on to the console, I am sure that you will find the solution.
come back if any doubt or query in that.

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

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