如何将phpdom保存到xml中?

时间:2018-12-23 16:53:06

标签: php xml dom

我正在使用此简单的php代码来解析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; 


}


?>

结果是:http://pocasi-dnes.cz/test-table.php

有什么方法可以获取简单的xml输出吗? 非常感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

不确定这是否是您想要的xml输出,但这会将您的数据显示为xml文件

<?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 ***/ 


header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8"?><data>';

foreach ($rows as $row) {
    echo '<lokalita>'.$cols->item(0)->nodeValue.'</lokalita>'; 
    echo '<celkem>'.$cols->item(1)->nodeValue.'</celkem>'; 
    echo '<druh>'.$cols->item(2)->nodeValue.'</druh>'; 
    echo '<novy>'.$cols->item(3)->nodeValue.'</novy>'; 
    echo '<provoz>'.$cols->item(4)->nodeValue.'</provoz>'; 
}
echo '</data>';

您可以使用所需的任何内容替换“数据”。

答案 1 :(得分:0)

您正在使用DOM API读取XML。可以使用相同的API创建和修改XML文档。使用DOMDocument::create*()方法创建节点,并使用DOMNodeappendChild()之类的insertBefore()方法附加/插入节点。

在以下示例中,我使用Xpath表达式优化了读取。

$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();

输出:

<?xml version="1.0"?>
<data>
  <umístění> 
    <lokalita>1. lokalita</lokalita>
    <celkem>2. celkem</celkem> 
  </umístění> 
</data>

答案 2 :(得分:0)

感谢大家的帮助。最后,我将所有信息合并为一个解决方案。