2013-03-07 79 views
0

XML我想創建網站page.I一個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的

編輯:

我有excecute後的問題這段代碼

$currentUrl = $this->getRequest()->getUri(); 
$domOb = new \DOMDocument(); 
$xml = $domOb->loadHTML(file_get_contents($currentUrl)); 

我得到

警告: 的file_get_contents (HTT p:// localhost /Surgery/web/app_dev.php/test): 未能打開流:HTTP請求失敗!在

在php.ini我allow_url_fopen = On

回答

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')]"); 
0

並以正確方式顯示:

$currentUrl = $this->getRequest()->getUri(); 
$domOb = new \DOMDocument(); 
$xml = $domOb->loadHTML(file_get_contents($currentUrl)); 
header('text/xml; charset=utf-8'); 
echo $xml;