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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里