Reputation: 7789
I want to parse following xml file,
Which is the best xml parser iOS. I want to parse it like SBJON library, I mean I want to parse it dynamically. For example store all POSITION_DATA element data in NSMutableDictionary and that dictionary should store in NSMutableArray.
If uses SBJON library then I will get first array of all data containing XML file then I will got to extract appropriate data from array.
Can I do like this in iOS using objective-C. I already uses TBXML library but I think it not suitable for given xml file format. Also I uses TouchXML library but it gives me compile time error "touchxml library and getting an error libxml/tree.h no such file or directory" .
How can I resolve this parsing problem?
Upvotes: 1
Views: 2763
Reputation: 5099
Here you have simple and great XML Parser to NSDictionary
Upvotes: 0
Reputation: 33428
I also suggest to read about GDataXML. It's easy and very fast. Here more information on how-to-read-and-write-xml-documents-with-gdataxml.
Hope it helps.
Upvotes: 0
Reputation: 14828
https://github.com/TouchCode/TouchXML
You'll thank me later. I can't stand NSXMLParser.
EDIT: Make sure to follow the install instructions included in the Docs folder of the download. Will show you how to get rid of the lib xml errors.
Upvotes: 1
Reputation: 8570
Look at the documentation for NSXMLParser
. It is included on iOS. You feed the XML data into an instance of it and then you become its delegate and then tell it to parse.
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:xmlData];
parser.delegate = self;
[parser parse];
Then implement
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
These three methods are called at tag open, encountering data within a tag and on tag close. You can listen out for the tags you want in the XML by comparing strings to the elementName
argument and store the data in objects as you see fit.
Look at the documentation from Apple for the full implementation detail. Why use a third party toolkit when there is a class built in that does what you need?
Upvotes: 3
Reputation: 9933
NSXMLParser is the standard Cocoa XML parsing tool. You will create an implementation of the NSXMLParserDelegate interface and then pass it to the NSXMLParser you create. I like to create a delegate for each of my model classes and then push and pop them as needed while parsing the XML tree. You can create callbacks from your delegate for an external class to do whatever with the objects your delegate creates.
Apple provides documentation here. Apple's SeismicXML example provides a good example of background parsing.
Upvotes: 3