tags:

views:

32

answers:

3

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?

+1  A: 

http://blancer.com/tutorials/i-phone/76999/parsing-xml-files/ hope this helps !!!

jknair
+1  A: 

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!

Alastair Pitts
will try TouchXML Alastair Pitts.
shishir.bobby
+1  A: 

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;
}
}

Javal Nanda
Thanks Javal, but i need to knw, StringToStore this var will store the value of abc right? if i want to assign each values of attribute, i mean,abc1, abc 2 abc3, than wat i hv to do? thanks for reply BTW
shishir.bobby
this will store the value between the tags . the code i have written will store value of <abc> tag i.e.(87C4A556-B7E5-5AE4-81FE-86BE0C6306E1). the xml u r having has a tag element and they are not called attributes. Attributes are for e.g <tag1 value="1" name="test"> . here value and name are attributes.
Javal Nanda
so for each tag you just need to add if condition checking the element name.
Javal Nanda
thats realy a nice explanation javal, i'll work on that part today....thanks mate
shishir.bobby
will this work?? -(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]; } if ([elementName isEqualToString:@"abc2"]) { usernameString = [[NSString alloc] init]; }}
shishir.bobby
i just put if condition in didstart method.Am i going in a right way.?
shishir.bobby