簡體   English   中英

如何從網頁創建XML

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM