为什么NSXMLParser在foundCharacters方法中拾取这个空格? [英] Why is NSXMLParser picking up this whitespace in the foundCharacters method?
问题描述
我正在学习使用iOS平台的 NSXMLParser
API,到目前为止,它非常易于使用。但是,我在foundCharacters方法中遇到了一个小问题。据我所知,它不应该拾取任何空格,因为 foundIgnorableWhitespace
方法应该捕获它,但它看起来像是。这是我的代码...
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"
)
这里是我正在处理的XML数据的片段...
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?
提前感谢您的帮助!
推荐答案
简单的解决方案是创建 didEndElement:
将 currentElement
设置为 kXMLElementOther
的方法。
The easy solution is to create a didEndElement:
method where you set currentElement
to kXMLElementOther
.
在 Ignorable White Space 的良好描述。 htmlrel =noreferrer>可忽略的空白区域。问题可能是您没有与文档关联的DTD。所以解析器实际上并不知道可忽略的空白是什么。 (它不仅仅是标签之间的空白区域,这可能与您的想法相同)所以它只是将所有内容都视为字符数据。
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.
这篇关于为什么NSXMLParser在foundCharacters方法中拾取这个空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!