I'm learning to use the NSXMLParser
API for the iOS platform and so far it's very easy to use. I'm having a small problem, however, in the foundCharacters method. As I understand it, it shouldn't pick up any whitespace since the foundIgnorableWhitespace
method is supposed to catch that, but it looks like it is. Here's the my code...
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
//We're at the start of a new data feed
if([elementName isEqualToString:@"data"])
{
if(listOfTimes != nil)
[listOfTimes release];
listOfTimes = [[NSMutableArray alloc] init];
}
else if ( [elementName isEqualToString:@"start-valid-time"]) {
currentElementType = kXMLElementTime;
return;
}
else {
currentElementType = kXMLElementOther;
}
//---------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(currentElementType == kXMLElementTime)
{
//We don't want anymore than three times
if ([listOfTimes count] >= 3)
return;
[listOfTimes addObject:string];
}
}
It basically stores three "time" elements in an array. The problem, however, is it seems to be picking up whitespace in the form of a newline. Here's the printout of the array in the console...
Printing description of listOfTimes:
(
"2010-08-21T22:00:00-05:00",
"\n ",
"2010-08-22T01:00:00-05:00"
)
and here's a snippet of the XML data I'm processing...
<time-layout time-coordinate="local" summarization="none">
<layout-key>k-p3h-n40-1</layout-key>
<start-valid-time>2010-08-21T22:00:00-05:00</start-valid-time>
<start-valid-time>2010-08-22T01:00:00-05:00</start-valid-time>
<start-valid-time>2010-08-22T04:00:00-05:00</start-valid-time>
<start-valid-time>2010-08-22T07:00:00-05:00</start-valid-time>
<start-valid-time>2010-08-22T10:00:00-05:00</start-valid-time>
.
.
.
Am I misunderstanding how this works?
Thanks in advance for your help!
The easy solution is to create a didEndElement:
method where you set currentElement
to kXMLElementOther
.
There is a good description of Ignorable White Space
at Ignorable White Space. The problem is probably that you do not have a DTD associated with your document. So the parser does not actually know what ignorable white space is. (It is not simply white space between tags, which is probably what you think) So it is simply treating everything as character data.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//whatever data i am getting from node i am appending it to the nodecontent variable
[nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(@"node content = %@",nodecontent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With