block into UITableCell
I'm using Hpple to parse HTML of a website onto my app. The parsing is working great but instead of all the contents of the tr block to be in one cell, each of the td elements in the tr block are table cells of their own. Here's what I mean. The TR Block:
<tr>
<td>2</td>
<td>down 1</td>
<td>1</td>
<td>5</td>
<td>Justin Bieber</td>
<td>What Do You Mean?</td>
</tr>
What it looks like in the app:
When I want it to actually look like this:
The code I'm using for the parsing looks like this:
- (void)loadSongs {
// 1
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.bbc.co.uk/radio1/chart/singles/print"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];
// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
// 3
NSString *tutorialsXpathQueryString = @"//tr/td";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
// 4
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
// 5
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
// 6
tutorial.title = [[element firstChild] content];
tutorial.peakPosition = ???;
}
// 8
_objects = newTutorials;
[self.tableView reloadData];
}
Source: (StackOverflow)
I am trying to parse the following website so I display the data like this on iOS:
Saturday 6th September
Causeway
Bond's Glen Raceway
11:00am
RO
Two Day Meeting
Two Separate Days
An example of the website:
<div id="main-column">
<h1>September</h1>
<table align="center"><col width="200"><col width="150"><col width="100"><col width="120"><col width="330"><col width="300">
<h2>Saturday 06 September</h2>
<tr id="table1">
<td><b>Club</b></td>
<td><b>Venue</b></td>
<td><b>Start Time</b></td>
<td><b>Meeting Type</b></td>
<td><b>Number of Days for Meeting</b></td>
<td><b>Notes</b></td>
</tr>
<tr id="table2">
<td>Causeway</td>
<td>Bond's Glen Raceway</td>
<td>11:00am</td>
<td>RO</td>
<td>Two Day Meeting,<br> Two Separate Days</td>
<td></td>
</tr>
<tr id="table3">
<td>West Waterford</td>
<td>Ballysaggart</td>
<td>11:00am</td>
<td>RO</td>
<td>Two Day Meeting,<br> One Meeting Over Two Days</td>
<td></td>
</tr>
So far I have managed to get all of the dates with the following code:
-(void)loadData {
NSURL *url = [NSURL URLWithString:@"http://www.national-autograss.co.uk/september.htm"];
NSData *htmlData = [NSData dataWithContentsOfURL:url];
TFHpple *htmlParser = [TFHpple hppleWithHTMLData:htmlData];
NSString *xpathQueryString = @"//h2";
NSArray *eventNodes = [htmlParser searchWithXPathQuery:xpathQueryString];
NSMutableArray *eventDates = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in eventNodes) {
NSString *date = [[element firstChild] content];
[eventDates addObject:date];
}
_objects = eventDates;
[self.tableView reloadData];
}
Is the Xpath query I need for the data in the table something like //table/tr/td? I tried this and I got an immediate error of adding a nil object to an array.
Or am I better to get all of the tables as separate elements and then parse individually for the data inside?
Any help, guides or ideas would be very much appreciated.
Source: (StackOverflow)
I have some xml that looks like this:
<menu>
<day name="monday">
<meal name="BREAKFAST">
<counter name="Bread">
<dish>
<name>Plain Bagel
<info name="Plain Bagel">
<serving>1 Serving (90g)</serving>
<calories>200</calories>
<caloriesFromFat>50</caloriesFromFat>
</info>
</name>
</dish>
<dish>
<name>Applesauce Coffee Cake
<info name="Applesauce Coffee Cake">
<serving>1 Slice-Cut 12 (121g)</serving>
<calories>374</calories>
<caloriesFromFat>104</caloriesFromFat>
</info>
</name>
</dish>
</counter>
</meal>
</day>
</menu>
And now I am trying to get the number of tags that are under the info
tag which should be three for the first info
tag which has the attribute of Plain Bagel
.
Like I said I am using Hpple parser for iOS. Here is what I have and am trying but can't quite get it to work.
- (void)getData:(NSData*)factData {
TFHpple *Parser = [TFHpple hppleWithHTMLData:factData];
NSString *XpathQueryString = @"//day[@name='monday']/meal[@name='BREAKFAST']/counter[@name='Bread']/dish/name/info[@name='Plain Bagel']";
NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString];
NSInteger count = Nodes.count;
NSLog(@"count: %ld", count);
for (TFHppleElement *element in Nodes) {
NSLog(@"count inside: %ld", element.children.count);
}
}
And the first count give 1. Which is right but count inside gives 7, which is where I get confused. And not sure why this happens. After I get inside the info
tag I want to loop through for each tag, serving, calories, and calories from fat and get each tags text. But Im not sure why it gives 7?
Thanks for the help in advance.
Source: (StackOverflow)
I'm starting my iPhone programming adventure, with a simple HTML scraping. I'm using the Hpple library to do the job, and I have a question...
Suppose I have the following html to parse...
<div>
<div> A <!-- Comment 1 --></div>
<div> B </div>
<div> C <!-- Comment 2 --></div>
</div>
How can I retrieve the commented parts? They don't show up on the objects... I was checking the docs but there's nothing pointing to that direction.. (also googling "hpple comment" doesn't produce the best results...).
thanks in advance.
Source: (StackOverflow)
I am using hpple to parse an HTML document. I followed Ray Wenderlich’s tutorial and have everything working fine for their example file. However, I need to change it up a bit to read a certain HTML file for my friends blog. The file is more complex than the example I have used so far. The relevant part of the file (full uploaded on gist is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<!-- snip -->
<div id="content" class="hfeed">
<div class="post-21443 post type-post status-publish format-standard hentry category-about-catherine">
<div class="postdate">
Apr <br />
6 <br />
2013
</div>
<h2 class="entry-title"><a rel='nofollow' href="http://catherinepooler.com/2013/04/stampnation-live-retreat-updates/" title="StampNation LIVE Retreat Updates" rel="bookmark">StampNation LIVE Retreat Updates</a></h2>
<div class="post-info"></div> <div class="entry-content">
<p><a rel='nofollow' href="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560.jpg" ><img class="aligncenter size-large wp-image-21444" alt="StampNation LIVE" src="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560-450x337.jpg" width="450" height="337" /></a></p> <p>StampNation LIVE is in full swing! We are having a wonderful time. I am taking a quick break from stamping and chatting to share a few photos with you.</p> <p>I think my favorite thing in getting ready for the retreat was setting up the Accessory Bar. Each attendee received a small galvanized bucket with their fully glittered initial on it to fill up at the bar. Awesome!</p>
<!-- snip -->
There are several of these sections within the file and I need to place all the
<h2 class = "entry-title">
(title="StampNation LIVE Retreat Updates") in an array. I have successfully placed the
<div class = "entry-content">
into an array by using the XPathQuery //div[@class = 'entry-content']/p
. However, I can’t seem to get the title without the code crashing due to an empty array. Obviously my XPathQuery is incorrect. This is what I tried.
//h2[@class = 'entry-title'] (: this crashed :)
//div[@class = 'post-21443.....']//h2[@class = 'entry-title'] (: this crashed too. ")
Along with a slew of other attempts!
Does anyone have any advice for me? I looked into many SO answers, and the examples that came with hpple, but I can not piece it together.
UPDATE: With Jens help I have changed the query to
NSString *postsXpathQueryString = @"//h2[@class = 'entry-title']/a";
This gets me an array, but I get this error as well now.
2013-04-08 10:26:30.604 HTML[12408:11303] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
* First throw call stack:
(0x210a012 0x1203e7e 0x20ac0b4 0x3852 0x2028fb 0x2029cf 0x1eb1bb 0x1fbb4b 0x1982dd 0x12176b0 0x2706fc0 0x26fb33c 0x2706eaf 0x2372bd 0x17fb56 0x17e66f 0x17e589 0x17d7e4 0x17d61e 0x17e3d9 0x1812d2 0x22b99c 0x178574 0x17876f 0x178905 0x9733ab6 0x181917 0x14596c 0x14694b 0x157cb5 0x158beb 0x14a698 0x2065df9 0x2065ad0 0x207fbf5 0x207f962 0x20b0bb6 0x20aff44 0x20afe1b 0x14617a 0x147ffc 0x1d2d 0x1c55)
libc++abi.dylib: terminate called throwing an exception
UPDATE 2
Fixed the error index beyond bounds by putting in an if statement when I reloadData. I get an array in my NSLog, but it is not putting it in my table view. Table view comes up empty!! But no more crash!!!
FINAL UPDATE
It is now working, Jens helped me get the query correct and then I just had to fill in the table view. I had set the array count to 20 because Ray's tut had a zillion entries. My friends blog, only had four! Thanks for all the help.
Source: (StackOverflow)
I am using hpple to try and grab a torrent description from ThePirateBay. Currently, I'm using this code:
NSString *path = @"//div[@id='content']/div[@id='main-content']/div/div[@id='detailsouterframe']/div[@id='detailsframe']/div[@id='details']/div[@class='nfo']/pre/node()";
NSArray *nodes = [parser searchWithXPathQuery:path];
for (TFHppleElement * element in nodes) {
NSString *postid = [element content];
if (postid) {
[texts appendString:postid];
}
}
This returns just the plain text, and not any of the URL's for screenshots. Is there anyway to get all links and other tags, not just plain text?
The piratebay is fomratted like so:
<pre>
<a rel='nofollow' href="http://img689.imageshack.us/img689/8292/itskindofafunnystory201.jpg" rel="nofollow">
http://img689.imageshack.us/img689/8292/itskindofafunnystory201.jpg</a>
More texts about the file
</pre>
Source: (StackOverflow)