2013-03-23 99 views
0

我有一个表格,其值从XML文件加载,并需要添加价格字段值来获得总数。我一直在看和看到的例子,但我不清楚。我怎样才能添加这个precio字段?如何添加使用PHP和xpath与SUM()DOM XML的值?

XML

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <libro> 
     <autor><![CDATA[Cervantes]]></autor> 
     <titulo><![CDATA[El Quijote]]></titulo> 
     <precio>30€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Calderón de la Barca]]></autor> 
     <titulo><![CDATA[La vida es sueño]]></titulo> 
     <precio>25€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Garcilaso de la vega]]></autor> 
     <titulo><![CDATA[Egoglas]]></titulo> 
     <precio>15€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Raymond Carver]]></autor> 
     <titulo><![CDATA[Catedral]]></titulo> 
     <precio>16€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[George Orwell]]></autor> 
     <titulo><![CDATA[1984]]></titulo> 
     <precio>10€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Fidor Dostoyevski]]></autor> 
     <titulo><![CDATA[Crimen y Castigo]]></titulo> 
     <precio>35€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Juan Ponce]]></autor> 
     <titulo><![CDATA[Cronica de la intervencion]]></titulo> 
     <precio>25€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Yukio Mishima]]></autor> 
     <titulo><![CDATA[Confesiones de una mascara]]></titulo> 
     <precio>22€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Elfriede Jelinek]]></autor> 
     <titulo><![CDATA[Deseo]]></titulo> 
     <precio>20€</precio> 
    </libro> 
    <libro> 
     <autor><![CDATA[Bram Stoker]]></autor> 
     <titulo><![CDATA[Dracula]]></titulo> 
     <precio>18€</precio> 
    </libro> 
</root> 

PHP

<?   
    ini_set('error_reporting', E_ALL^E_NOTICE); 
    ini_set('display_errors','on'); 
      ob_start(); 
      $dom=new DOMDocument(); 
      $dom->load('xml/libros.xml'); 
      $libros=$dom->getElementsByTagName('libro'); 
      $i=0; 
      echo "<table cellspacing='2'>"; 
      foreach ($libros as $libro) { 
       $autores=$libro->getElementsByTagName('autor'); 
       $autor=$autores->item(0)->nodeValue; 
       $titulos=$libro->getElementsByTagName('titulo'); 
       $titulo=$titulos->item(0)->nodeValue; 
       $precios=$libro->getElementsByTagName('precio'); 
       $precio=$precios->item(0)->nodeValue; 
       echo "<tr id='fila".$i."'>"; 
       echo "<td>".$autor."</td>"; 
       echo "<td>".$titulo."</td>"; 
       echo "<td>".$precio."</td>"; 
       echo "</tr>"; 
       $i++; 
      } 
      echo "</table>"; 
      $tabla=ob_get_contents(); 
      ob_end_clean(); 
      $console="Toda va ok"; 
      $aValores=array('tabla'=> $tabla,'console'=>$console); 
      echo json_encode($aValores); 
?> 

回答

0

根据该文件,在XPath的应该是sum(//precio)
虽然我无法让它工作。所以,我选择了一个不太优雅,但功能的方法:

$xml = simplexml_load_string($xmlstr); 
$sum = 0; 
$h = "<table>"; 

foreach ($xml->libro as $book) { 

    $h .= "<tr><td>".$book->autor."</td><td>".$book->titulo."</td><td>".$book->precio."</td></tr>"; 

    $sum += $book->precio; 
} 

$h .= "<tr><td colspan=\"2\">sum:</td><td>$sum</td></tr></table>"; 
echo $h; 

我喜欢simplexmldom。如果你看我的代码,你会明白为什么;-)
这里是一个生活演示:http://codepad.viper-7.com/q3drF5

+0

伟大的解决方案! – jal 2013-03-23 21:37:23