duanbu9345
duanbu9345
2018-12-23 16:53
浏览 74

如何将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
    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>
    
    点赞 评论
  • doww38701
    doww38701 2018-12-23 17:07

    Not sure if it's the xml output you want , but this will display your data as xml file

    <?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>';
    

    you can replace "data" with anything you want.

    点赞 评论
  • douyan7916
    douyan7916 2018-12-27 09:20

    Thanks everybody for helping me. I finally merge all information into one solution.

    点赞 评论

相关推荐