Hi,
Have been trying several days to parse the following html code (notice that there is not a real hierarchal tree structure). Everything is pretty much on the same level.
<p><span class='one'>week number</span></p>
<p><span class='two'>day of the week</span></p>
<table class='spreadsheet'>
table data
</table>
<p><span class='two'>another day of the week</span></p>
<table class='spreadsheet'>
table data
</table>
<p><span class='one'>another week number</span></p>
ETC
What I basically want to do is, to go through each dom element, check whether it is a week, if it is, add all the days of the week to that specific week, and add all the table data to the corresponding day of the week. So something of the following structure:
array {
31 => array {
monday => array {
data => table data
}
tuesday => array {
data => table data
}
}
32 => array {
monday => array {
data => table data
}
tuesday => array {
data => table data
}
}
}
This is my PHP code that I have so far.
$d = new DomDocument;
@$d->loadHtml($html);
$xp = new DomXpath($d);
$res = $xp->query( "//*[@class='one' or @class='two' or @class='spreadsheet']" );
foreach ($res as $dn) {
$nodes = $dn->childNodes;
foreach ($nodes as $node) {
if ($node->nodeValue != "") {
echo $node->nodeValue;
}
}
}
I have been tipped by some people here at stackoverflow to use Xpath in order to achieve this, the above code handles each node separately. What I think I need to be doing is get all the "week" nodes, and than get their next sibling, check from there wether it is a day, if so add this to that array, if it is a "week" node, create a new array etc etc
I have been tearing my hair out the past few days with this, so any help/push in the right direction would be very much appreciated!!!
Cheers, Dandoen