0

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...

JRoss
  • 177
  • 10
  • 23
  • When you log xmlCheck for the url you are having trouble with, does it show valid xml? If you put `NSLog(@"parserError = %@", [parser parserError]);` after the `[parser parse];` line, what does it say? –  Apr 12 '11 at 15:36
  • I do receive the following error: parserError = Error Domain=NSXMLParserErrorDomain Code=68 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 68.)" I am assuming this means that my URL did not contain valid XML? How do you know if a website is in XML, HTML, or something else? What do coders generally do when wishing to parse a URL that is not in XML? – JRoss Apr 13 '11 at 05:51
  • For parsing html, you might have more success using libxml2 which is included in the iOS sdk as mentioned [on this page](http://developer.apple.com/library/ios/#documentation/Miscellaneous/Conceptual/iPhoneOSTechOverview/CoreServicesLayer/CoreServicesLayer.html) and search SO. For example: http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone –  Apr 13 '11 at 13:41

1 Answers1

0

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>`
NWCoder
  • 5,296
  • 1
  • 26
  • 23
  • Since I do not control this URL, would the following be the best approach: 1. convert my (NSMutableData *)xmlData into a string (with your start tag in the beginning) 2. convert the new string into an NSURL object 3. parse with the "initWithContentsOfURL" instance method? – JRoss Apr 13 '11 at 05:58
  • When I update the 'connectionDidFinishLoading' method to parse my converted URL object, I receive the NSXML Error: "NSXMLParserErrorDomain error 5" – JRoss Apr 13 '11 at 12:42