No Elements Found When Parsing?
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开发者_JS百科 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...
The html isn't valid XML, but I think that's an easy fix, add the xml start tag like:
<?xml version="1.0" encoding="utf-8"?>
<tr style=" ">
snipped for space...
</tr>`
精彩评论