Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML in Cocoa

Today I am looking into how to make a simple XML parser in Cocoa (for the desktop). I am thinking of using NSXMLParser to parse the data, but am not quite sure where to start. The XML file on the web doesn't have the much data in it, just a simple listing with a few things that I need to save into a variable. Does anyone have any suggestions on how to do this, as the online documentation about this isn't making too much sense.

Thanks for any help!

EDIT The reason why I am wanting to create an XML parser is to get information from a MYSQL database on a server to the client application. If there is some better way to do this besides a XML parser, please let me know!

like image 783
PF1 Avatar asked Jul 06 '09 23:07

PF1


People also ask

What is XML data parsing?

Definition. XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.

What is XML parser in Java?

The XML parser for Java is a standalone XML component that parses an XML document (and possibly also a standalone DTD or XML Schema) so that your program can process it. This section contains the following topics: Using the XML Parser for Java: Basic Process. Running the XML Parser Demo Programs.

What is PHP XML?

PHP XML Parser Introduction XML is a data format for standardized structured document exchange. More information on XML can be found in our XML Tutorial. This extension uses the Expat XML parser. Expat is an event-based parser, it views an XML document as a series of events.


1 Answers

Here's how it works:

There's a class called NSXMLParser. It's used to parse XML files. However, NSXMLParser is stupid. All it knows how to do is parse XML, but it doesn't know what it's supposed to do with the information it finds.

Enter a delegate. A delegate is like a nanny. Since the XMLParser doesn't have a clue what to do with the information it finds, it goes and asks its delegate about each and every thing: "Hey! I started parsing a document! Am I supposed to do anything?" "Hey! I found some CDATA! What am I supposed to do with it!" "Hey! I found another tag!" "Hey! I found a closing tag!", and so on. All of these "Hey!" statements are delegate methods, or in other words, they are optional methods that a delegate object may choose to implement. Usually (but not always), the object that creates the NSXMLParser is also the delegate, but that doesn't have to be the case.

So you might have something like this:

NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:someURLToAnXMLFile];
[parser setDelegate:self];
[parser parse];
[parser release];

Then in that same object (self), you might have some of these methods:

- (void)parserDidStartDocument:(NSXMLParser *)parser {
  //the parser started this document. what are you going to do?
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
  //the parser found an XML tag and is giving you some information about it
  //what are you going to do?
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  //the parser found some characters inbetween an opening and closing tag
  //what are you going to do?
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
  //the parser finished. what are you going to do?
}

There are a whole bunch of these methods listed in the documentation. Simply go to the NSXMLParser class reference, and they're all listed under the "Delegate Methods" section. Once you get the hang of it, NSXMLParser is pretty easy to use. It is a SAX Parser, which means it's event-driven parser. It finds stuff, and it tells you about it.

like image 81
Dave DeLong Avatar answered Sep 24 '22 15:09

Dave DeLong