使用 PHP Simple HTML Dom 解析器遍历表行直到已知元素
好吧,我正在尝试使用 PHP Simple HTML DOM Parser 从此 HTML 表构建 xml feed。
<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>
我的目标是提取日期,然后提取以下行,直到下一个日期。这样我就可以为每个日期构建一个 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>
我目前已经从 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>';
}
我将如何获取每个固定装置的球队名称等,直到下一个比赛日日期?
Ok im trying to build an xml feed from this HTML table using PHP Simple HTML DOM Parser.
<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>
What I am aiming to do is extract the Date and then the following rows up until the next date. so that I can build an XML node as such for each of the dates.
<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>
I have at present each of the dates from the html and built their respective xml nodes
$dateNodes = $html->find('table tr td[colspan="5"] strong');
foreach($dateNodes as $date){
echo '<matchday day="'.trim($date->innertext).'">';
// FIXTURES
// END FIXTURES
echo '</matchday>';
}
How would i go about getting the team names etc for each fixture up until the next matchday date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相反,如果SimpleHtmlDom(我认为这是一个糟糕的库),你可以使用 XSLT 转换 和 PHP 的本机 XSLT 处理器:
然后只需使用 http://php.net/manual/en/xsltprocessor.transformtoxml.php 将 HTML 转换为 XML:
Codepad 上的演示
除了使用 XSLT 之外,您还可以使用 PHP 的本机 DOM 扩展来实现:
Codepad 上的演示
Instead if SimpleHtmlDom (which I believe is a craptaculous library), you can use an XSLT transformation and PHP's native XSLT processor:
Then just use the code given in the example at http://php.net/manual/en/xsltprocessor.transformtoxml.php to transform your HTML to the XML:
Demo at Codepad
In addition to using XSLT, you can also do it with PHP's native DOM extension:
Demo at Codepad