我想从网页创建一个XML文件。我的网站有例如这个表
<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>
我尝试了类似的东西,但它不起作用。如何从网页中的表加载数据?
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$html = $domOb->loadHTMLFile($currentUrl);
我在localhost上工作并使用Symfony2
编辑:
执行此代码后我有问题
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
我得到了
在php.ini中警告: 的file_get_contents(HTTP://
localhost
/Surgery/web/app_dev.php/test): 无法打开流:HTTP请求失败!在
我有allow_url_fopen = On
答案 0 :(得分:0)
您需要为loadHTML
对象调用$domOb
方法并传递网页内容:
// disable libxml warnings
libxml_use_internal_errors(true);
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
@$domOb->loadHTML(file_get_contents($currentUrl));
然后您可以使用$domOb
对象来解析html,例如,为了获得您可以执行以下操作的表:
$xpath = new DOMXPath($domOb);
$items = $xpath->evaluate("//table[contains(@class, 'table')]");
答案 1 :(得分:0)
并以正确的方式显示:
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
header('text/xml; charset=utf-8');
echo $xml;