解析时未找到元素?
我是 iPhone 编程新手,但是,我已经研究我的问题有一段时间了,但一直无法找到解决方案...
基本上,我试图解析一个我无法控制的 URL,其中包含以下内容其页面源中所需的信息:
<tr style=" ">
<td valign="top" style=" ">
<p style=" ">
4/10/2011
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
-18
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
21%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
39%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
45%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
54%
</p>
</td>
</tr>`
正确创建与站点的连接后,我将数据放在一起并尝试解析:
//This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[xmlData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
/*
//We are just checking to make sure we are getting the XML
NSString *xmlCheck = [[[NSString alloc] initWithData:xmlDat encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"xmlCheck = %@", xmlCheck);
//Check was Successful!
*/
//Create the parser object with the data received from the web service
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
//Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of
//its delegate messages sent to it before this line finishes execution - it is blocking
[parser parse];
//The parser is done (it blocks until done), you can release it immediately
[parser release];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
NSLog(@"StartedParsing");
//if ([elementName isEqualToString:@"tr"]) {
if (elementName) {
NSLog(@"found element!");
titleString = [[NSMutableString alloc] init];
}
}
您会注意到,首先我尝试搜索元素“tr”,但随后切换到寻找URL 中的任何元素。我的控制台窗口没有打印任何内容,这让我认为我的解析器没有找到任何元素。
现在,如果我在以下网站上尝试相同的代码: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml,发现了许多不同的元素。这让我相信 URL 可能需要采用某种格式。有人可以帮助我吗?
提前致谢!
PS - 我还实现了“foundCharacters”和“didEndElement”解析器函数,但无法使用它们,因为我的解析器似乎没有找到任何元素......
I am new to iPhone programming, however, I have been researching my problem for some time now and have not been able to find a solution...
Basically, I am trying to parse a URL that I do not control, which has the following desired info in its Page Source:
<tr style=" ">
<td valign="top" style=" ">
<p style=" ">
4/10/2011
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
-18
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
21%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
39%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
45%
</p>
</td>
<td valign="top" style=" ">
<p style=" ">
54%
</p>
</td>
</tr>`
After properly creating a connection with the site, I put my data together and attempt to parse:
//This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[xmlData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
/*
//We are just checking to make sure we are getting the XML
NSString *xmlCheck = [[[NSString alloc] initWithData:xmlDat encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"xmlCheck = %@", xmlCheck);
//Check was Successful!
*/
//Create the parser object with the data received from the web service
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
//Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of
//its delegate messages sent to it before this line finishes execution - it is blocking
[parser parse];
//The parser is done (it blocks until done), you can release it immediately
[parser release];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
NSLog(@"StartedParsing");
//if ([elementName isEqualToString:@"tr"]) {
if (elementName) {
NSLog(@"found element!");
titleString = [[NSMutableString alloc] init];
}
}
You will notice that at first I attempted to search for the element, 'tr', but then switched to looking for any element in the URL. Nothing is printed to my Console Window, which makes me think my parser is not finding any elements.
Now, if I attempt the same code on the following website: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml, a number of different elements are found. This leads me to believe the URL may need to be in a certain format. Can someone help me??
Thanks in Advance!
P.S. - I have also implemented the 'foundCharacters' and 'didEndElement' Parser Functions, but have not been able to use them as my parser doesn't appear to be finding any elements...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
html 不是有效的 XML,但我认为这是一个简单的修复方法,添加 xml 开始标记,例如:
The html isn't valid XML, but I think that's an easy fix, add the xml start tag like: