dongyou8368 2013-03-07 14:29
浏览 28

如何从网页创建XML

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

  • 写回答

2条回答 默认 最新

  • dou12754 2013-03-07 14:33
    关注

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

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能