I need to scrape some website data from a table on a website and create an XML or JSON document that will be used by an app. and i have some problem getting the below data.
The table looks like this:
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#DDEEFF" width="100%">
<tr>
<td width="20%" ><font face="Verdana, Arial" size="1">SRC</a></td></font>
<td width="58%" ><font face="Verdana, Arial" size="1"><a href="http://example.com/this/news?id=1&by=today" onMouseOver="a('Open Bulletin');return true" onMouseOut="b()">Welcome</font></a></td>
<td width="17%" align="center"><font face="Verdana, Arial" size="1">Event</td></font>
</tr>
<tr>
<td width="20%" ><font face="Verdana, Arial" size="1">FMD</a></td></font>
<td width="58%" ><font face="Verdana, Arial" size="1"><a href="http://example.com/this/news?id=2&by=today" onMouseOver="a('Open Bulletin');return true" onMouseOut="b()">Another News</font></a></td>
<td width="17%" align="center"><font face="Verdana, Arial" size="1">Updates</td></font>
</tr>
</td>
And I would like to create an XML feed or JSON that looks like this:
<bulletins>
<title>Welcome</title>
<id>1</id>
<type>News</type>
</bulletins>
<bulletins>
<title>Another News</title>
<id>2</id>
<type>Updates</type>
</bulletins>
Here is my current code :
<?php
$body = explode('<table border="0" cellpadding="3" cellspacing="0" bgcolor="#DDEEFF" width="100%">', $html);
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><xml />");
$rows = array();
foreach (array_slice(explode('<tr>', end($body)), 1) as $row)
{
preg_match('#<a.*?href="(.*?)".*?>(.*?)</a>#i', $row, $title);
preg_match('/<a.*href="(.*)".*>(.*)<\/a>/iU', $row, $id);
// preg_match('/type">([^<]+)<\/td>/', $row, $type);
$node = $xml->addChild('bulletins');
$node->addChild('title', $title[1]);
$node->addChild('id', $id[1]);
// $node->addChild('type', $due[1]);
}
header('Content-Type: text/xml');
echo $xml->asXML();
?>
But the problem i got this
<xml>
<bulletins>
<title>http://example.com/this/news?id=1</title>
<id>http://example.com/this/news?id=1</id>
</bulletins>
<bulletins>
<title>http://example.com/this/news?id=2</title>
<id>http://example.com/this/news?id=2</id>
</bulletins>
</xml>