[英]How create XML from webpage
我想从网页创建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);
我在本地主机上工作并使用Symfony2
编辑:
执行此代码后出现问题
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
我懂了
警告:file_get_contents(http://
localhost
/Surgery/web/app_dev.php/test):无法打开流:HTTP请求失败! 在
在php.ini中,我具有allow_url_fopen = On
您需要为$domOb
对象调用loadHTML
方法并传递网页内容:
// 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')]");
并以正确的方式显示它:
$currentUrl = $this->getRequest()->getUri();
$domOb = new \DOMDocument();
$xml = $domOb->loadHTML(file_get_contents($currentUrl));
header('text/xml; charset=utf-8');
echo $xml;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.