2

i need your help. This is the first time i try to parse HTML and i am running through some problems. I followed Rays Tutorial about HTML parsing. He uses hpple. The file i want to parse is a lot more complicated than his. I want to excract some variables through the code below:

<div id="status">
<div id="loading" style="display:none">Error:<br />Connection to demo board was lost.</div>
<div id="display">
    <span style="float:right;font-size:9px;font-weight:normal;padding-top:8px;text-indent:0px">(click to toggle)</span>
    <p>LEDs:<br /><span class="leds">
    <!-- <a id="led7" onclick="newAJAXCommand('leds.cgi?led=7');">&bull;</a>
    <a id="led6" onclick="newAJAXCommand('leds.cgi?led=6');">&bull;</a>
    <a id="led5" onclick="newAJAXCommand('leds.cgi?led=5');">&bull;</a>
    <a id="led4" onclick="newAJAXCommand('leds.cgi?led=4');">&bull;</a>
    <a id="led3" onclick="newAJAXCommand('leds.cgi?led=3');">&bull;</a>
    <a id="led2" onclick="newAJAXCommand('leds.cgi?led=2');">&bull;</a> -->
    <a id="led1" onclick="newAJAXCommand('leds.cgi?led=1');">&bull;</a>
    <!-- <a id="led0">&bull;</a> -->
    </span></p>
    <p>Buttons:<br />
    <!-- <span id="btn3">?</span> &nbsp; 
    <span id="btn2">?</span> &nbsp; 
    <span id="btn1">?</span> &nbsp;  -->
    <span id="btn0">?</span></p>
    <p>Potentiometer: <span id="pot0" style="font-weight:normal">?</span></p> 
    <p>Temperature: <span id="temp0" style="font-weight:normal">?</span></p> 
</div>

So far i can get LEDs, Buttons, Potentiometer and Temperature. I can not get their values . (the values of those specific 4 fields).

I am using the code below:

- (void)loadTutorials {
// 1
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://192.168.0.112/"]; 
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];

// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];

// 3
NSString *tutorialsXpathQueryString = @"//div[@id='display']/p"; 
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];
    NSLog(@"Object=%@",element);
    // 6
    tutorial.title = [[element firstChild] content]; 

    // 7
    tutorial.url = [element objectForKey:@"???"]; //??? 
}

// 8
_objects = newTutorials;
[self.tableView reloadData];

}

I suspect that the problem is the objectForKey: But i am not sure. I tested all sort of keys i could imagine. Any help would be most welcome.

Pantelis Proios
  • 1,359
  • 1
  • 20
  • 32
  • 1
    Not an answer to your question but I spent weeks trying to get HTML parsing working reliably with `hpple` before eventually giving up and taking a completely different approach. – Robert Feb 22 '13 at 15:33
  • What approach did you took? – Pantelis Proios Feb 22 '13 at 17:06
  • Well, my problem was rather specific but I ended up solving by utilising an ASP.Net web service, as mentioned in this question: http://stackoverflow.com/questions/13400918/access-documents-on-secure-web-server-from-ios-device – Robert Feb 22 '13 at 18:43

0 Answers0