I want create a XML file from web page.I have site with for example this table
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/Surgery/web/app_dev.php/workers/13/show">13</a></td>
<td>aa</td>
<td>aaaa</td>
<td>aaaa</td>
</tr>
</tbody>
</table>
I tried something like that, but it doesn't work. How to load data from table in web page ?
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$html = $domOb->loadHTMLFile($currentUrl);
I work on localhost and use Symfony2
EDIT:
I have problem after excecute this code
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
I get
Warning: file_get_contents(http://
localhost
/Surgery/web/app_dev.php/test): failed to open stream: HTTP request failed! in
in php.ini I have allow_url_fopen = On
You need to call loadHTML
method for $domOb
object and pass the content of a webpage:
// disable libxml warnings
libxml_use_internal_errors(true);
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
@$domOb->loadHTML(file_get_contents($currentUrl));
Then you can use $domOb
object to parse html, for example to get your table you can do following:
$xpath = new DOMXPath($domOb);
$items = $xpath->evaluate("//table[contains(@class, 'table')]");
And display it with a correct manner:
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
header('text/xml; charset=utf-8');
echo $xml;