使用PHP简单HTML Dom解析器遍历表行直到已知元素
好的,我正在尝试使用PHP简单HTML-DOM解析器从这个HTML表构建一个xml提要使用PHP简单HTML Dom解析器遍历表行直到已知元素,php,xml,simple-html-dom,Php,Xml,Simple Html Dom,好的,我正在尝试使用PHP简单HTML-DOM解析器从这个HTML表构建一个xml提要 <table> <tr><td colspan="5"><strong>Saturday October 15 2011</strong></td></tr> <tr><td>Team 1</td> <td>vs</td> <td>Tea
<table>
<tr><td colspan="5"><strong>Saturday October 15 2011</strong></td></tr>
<tr><td>Team 1</td> <td>vs</td> <td>Team 7</td> <td>3:00 pm</td></tr>
<tr><td>Team 2</td> <td>vs</td> <td>Team 12</td> <td>3:00 pm</td></tr>
<tr><td>Team 3</td> <td>vs</td> <td>Team 8</td> <td>3:00 pm</td></tr>
<tr><td>Team 4</td> <td>vs</td> <td>Team 10</td> <td>3:00 pm</td></tr>
<tr><td>Team 5</td> <td>vs</td> <td>Team 11</td> <td>3:00 pm</td></tr>
<tr><td colspan="5"><strong>Monday October 17 2011</strong></td></tr>
<tr><td>Team 6</td> <td>vs</td> <td>Team 9</td> <td>7:45 pm</td></tr>
<tr><td colspan="5"><strong>Saturday October 22 2011</strong></td></tr>
<tr><td>Team 7</td> <td>vs</td> <td>Team 12</td> <td>3:00 pm</td></tr>
<tr><td>Team 1</td> <td>vs</td> <td>Team 2</td> <td>3:00 pm</td></tr>
<tr><td>Team 8</td> <td>vs</td> <td>Team 4</td> <td>3:00 pm</td></tr>
<tr><td>Team 3</td> <td>vs</td> <td>Team 6</td> <td>3:00 pm</td></tr>
<tr><td>Team 9</td> <td>vs</td> <td>Team 5</td> <td>3:00 pm</td></td></tr>
<tr><td>Team 10</td> <td>vs</td> <td>Team 11</td> <td>3:00 pm</td></tr>
</table>
2011年10月15日星期六
第一队对第七队下午3:00
第二队对第十二队下午3:00
第三队对第八队下午3:00
第四队对第十队下午3:00
第五队对第十一队下午3:00
2011年10月17日星期一
第六队对第九队下午7:45
2011年10月22日星期六
第七队对第十二队下午3:00
第一队对第二队下午3:00
第八队对第四队下午3:00
第三队对第六队下午3:00
第九队对第五队下午3:00
10队对11队下午3:00
我的目标是提取日期,然后提取以下行,直到下一个日期。这样我就可以为每个日期构建一个XML节点
<matchday date="Saturday October 15 2011">
<fixture>
<hometeam>Team 1</hometeam>
<awayteam>Team 7</awayteam>
<kickoff>3:00 pm</kickoff>
</fixture>
<fixture>
<hometeam>Team 2</hometeam>
<awayteam>Team 12</awayteam>
<kickoff>3:00 pm</kickoff>
</fixture>
</matchday>
第一队
第7队
下午三点
第二小组
第12队
下午三点
目前,我已经从html中获取了每个日期,并构建了它们各自的xml节点
$dateNodes = $html->find('table tr td[colspan="5"] strong');
foreach($dateNodes as $date){
echo '<matchday day="'.trim($date->innertext).'">';
// FIXTURES
// END FIXTURES
echo '</matchday>';
}
$dateNodes=$html->find('table tr td[colspan=“5”]strong');
foreach($dateNodes作为$date){
回声';
//固定装置
//端部固定装置
回声';
}
在下一个比赛日之前,我如何获得每个赛程的球队名称等?如果可以使用and:
除了使用XSLT,还可以使用PHP的本机DOM扩展:
$xml = new DOMDocument;
$xml->loadHtmlFile('YourHtmlFile.xml');
$xp = new DOMXPath($xml);
$new = new DOMDocument('1,0', 'utf-8');
$new->appendChild($new->createElement('matchdays'));
foreach ($xp->query('//table/tr/td[@colspan=5]/strong') as $gameDate) {
$matchDay = $new->createElement('matchday');
$matchDay->setAttribute('date', $gameDate->nodeValue);
foreach ($xp->query(
sprintf(
'//tr[
not(td[@colspan]) and
preceding-sibling::tr[td[@colspan]][1]/td/strong/text() = "%s"
]',
$gameDate->nodeValue
)
) as $gameData) {
$tds = $gameData->getElementsByTagName('td');
$fixture = $matchDay->appendChild($new->createElement('fixture'));
$fixture->appendChild($new->createElement(
'hometeam', $tds->item(0)->nodeValue)
);
$fixture->appendChild($new->createElement(
'awayteam', $tds->item(2)->nodeValue)
);
$fixture->appendChild($new->createElement(
'kickoff', $tds->item(3)->nodeValue)
);
}
$new->documentElement->appendChild($matchDay);
}
$new->formatOutput = true;
echo $new->saveXML();
是否可以将外部url加载到$xml->loadHtmlFile('YourHtmlFile.xml');如果我想解析另一个网站来检索它的data@ChrisMJ对如果您尚未接受答复,请参阅。你能澄清一下你在答案中寻找的是什么,以及为什么给出的答案不能让你满意吗。
$xml = new DOMDocument;
$xml->load('YourSourceFile.xml');
$xsl = new DOMDocument;
$xsl->load('YourStyleSheet.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
$xml = new DOMDocument;
$xml->loadHtmlFile('YourHtmlFile.xml');
$xp = new DOMXPath($xml);
$new = new DOMDocument('1,0', 'utf-8');
$new->appendChild($new->createElement('matchdays'));
foreach ($xp->query('//table/tr/td[@colspan=5]/strong') as $gameDate) {
$matchDay = $new->createElement('matchday');
$matchDay->setAttribute('date', $gameDate->nodeValue);
foreach ($xp->query(
sprintf(
'//tr[
not(td[@colspan]) and
preceding-sibling::tr[td[@colspan]][1]/td/strong/text() = "%s"
]',
$gameDate->nodeValue
)
) as $gameData) {
$tds = $gameData->getElementsByTagName('td');
$fixture = $matchDay->appendChild($new->createElement('fixture'));
$fixture->appendChild($new->createElement(
'hometeam', $tds->item(0)->nodeValue)
);
$fixture->appendChild($new->createElement(
'awayteam', $tds->item(2)->nodeValue)
);
$fixture->appendChild($new->createElement(
'kickoff', $tds->item(3)->nodeValue)
);
}
$new->documentElement->appendChild($matchDay);
}
$new->formatOutput = true;
echo $new->saveXML();