duanbu9345 2018-12-23 16:53
浏览 84
已采纳

如何将phpdom保存到xml中?

I am using this simple php code to parse some information from html

 <?php 
/*** a new dom object ***/ 
$dom = new domDocument; 
$dom->loadHTMLFile('https://www.example.com/prehled.php'); 
/*** load the html into the object ***/ 
$dom->loadHTML($html); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 

/*** get all rows from the table ***/ 
$rows = $tables->item(1)->getElementsByTagName('tr');  

/*** loop over the table rows ***/ 
foreach ($rows as $row) {
/*** get each column by tag name ***/ 
$cols = $row->getElementsByTagName('td'); 


  echo 'lokalita: '.$cols->item(0)->nodeValue.'<br />'; 
  echo 'celkem: '.$cols->item(1)->nodeValue.'<br />'; 
  echo 'druh: '.$cols->item(2)->nodeValue.'<br />'; 
  echo 'novy: '.$cols->item(3)->nodeValue.'<br />'; 
  echo 'provoz: '.$cols->item(4)->nodeValue; 


}


?>

and here is the result: http://pocasi-dnes.cz/test-table.php.

Is there any way how to get simple xml output? Thank you very much for any help.

  • 写回答

3条回答 默认 最新

  • doushi5024 2018-12-23 18:21
    关注

    You're using the DOM API to read the XML. The same API can be used to create and modify XML documents. Create a node using a DOMDocument::create*() method and append/insert it using DOMNode methods like appendChild() or insertBefore().

    In the following examples I optimized the reading using Xpath expressions.

    $html = <<<'HTML'
    <table>
      <tr>
        <td>1. lokalita</td>
        <td>2. celkem</td>
      </tr>
    </table>
    HTML;
    
    // the source HTML document
    $source = new \DOMDocument();
    // loadHTMLFile() for files / loadHTML() for strings
    $source->loadHTML($html);
    $xpath = new \DOMXpath($source);
    
    // the new target XML document
    $target = new \DOMDocument();
    // add a root element
    $target->appendChild($target->createElement('data'));
    
    // iterate the table rows in the source
    foreach ($xpath->evaluate('//table/tr') as $row) {
        // an element for the group
        $target->documentElement->appendChild(
            $locationNode = $target->createElement('umístění')
        );
        // first table cell
        $locationNode
          ->appendChild($target->createElement('lokalita'))
          ->appendChild($target->createTextNode($xpath->evaluate('string(td[1])', $row)));
        // second table cell
        $locationNode
          ->appendChild($target->createElement('celkem'))
          ->appendChild($target->createTextNode($xpath->evaluate('string(td[2])', $row)));
        // ...   
    }
    
    $target->formatOutput = TRUE;
    echo $target->saveXML();
    

    Output:

    <?xml version="1.0"?>
    <data>
      <umístění> 
        <lokalita>1. lokalita</lokalita>
        <celkem>2. celkem</celkem> 
      </umístění> 
    </data>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码