Re: Getting started with XML and XMLParser
Re: Getting started with XML and XMLParser
- Subject: Re: Getting started with XML and XMLParser
- From: Roarke Lynch <email@hidden>
- Date: Sun, 9 Nov 2003 15:01:03 -0500
On Nov 9, 2003, at 11:26 AM, Kurt Marek wrote:
>
I need to parse an XML file and I had previously written a cheesy hack
>
using a lot of NSScanner code to get through it. Now that XMLParser is
>
available I would like to use it to clean up my code and make things a
>
bit easier to read and undoubtedly more efficient.
>
>
I read through the class description, but can't quite figure out how
>
to actually extract the data I am after. The class I wrote previously
>
would just scan up to the beginning tag and then put the data into a
>
string until it reached the ending tag. This got a little messy when I
>
had to pull out repeating elements that were defined by the same set
>
of beginning and ending tags. I am assuming that this is something the
>
XMLParser class can do without difficulty.
>
>
I have figured out how to get my XML file into the parser, but I can't
>
see how to actually extract the data from between the tags. Could
>
someone point me in the right direction?
What info between the tags? The attributes, the children , or the
CDATA/PCDATA?
Attributes are just returned as a dictionary to the delegate with:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName: (NSString*)qualifiedName attributes:(NSDictionary
*)attributeDict
As to the children, I just put an NSMutableArray called _nodestack into
my delegate. When it gets the above call it creates the Node
representation and adds it to the last object in the _nodestack as a
child. When the delegate get the didEndElement call it removes the last
object in the _nodestack.
For CDATA/PCDATA I had a similar approach, in responding to
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
or
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
the delegate creates a leaf node and adds it as a child to the last
object in the stack without placing the leaf node onto the end of the
stack.
Roarke Lynch
-------------------------------
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.