0

I have been trying to do XML parsing in iOS. But I was not able to parse it unfortunately.

Can you help me with this. I was using a SAX parser using NSXML.

The data is

<category>
<cat id="info" name="id1" value="val1" />
<cat id="info" name="id2" value="val2" />
</category>

How do i get the values using SAX parser? Any help would be appreciated.

Thanks

Answer-

(To get the attributes) I used it this way (attributedict is the NSDictionary object in didStartElement method)

NSString *attr = [attributeDict valueForKey:@"name"];
if([attr isEqualToString:@"id1"])
{
NSLog("%@", [attributeDict valueForKey:@"value"]);
}
nithinreddy
  • 6,167
  • 4
  • 38
  • 44
  • "I was using a SAX parser". Err, maybe you should tell us which SAX parser and what problems you were having. – Gruntcakes Jun 17 '12 at 15:33

1 Answers1

1

you need to init an NSXML instance with Data then use the parse method. You need to set up a NSXMLParserDelegate protocol, then using the following methods, you'll be able to retrieve information :

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

You should read the apple documentation about it :

NSXMLParser

NSXMLParserDelegate Protocol

You'll find Apple examples in the links.

moxy
  • 1,634
  • 1
  • 11
  • 19