2012-03-02 67 views
0

我使用一个XML文件,将所有的数据(数字)从XML文件,例如:在“点”节点使用PHP

 <result> 
     <team> 
     <name>arsenal</name> 
     <games>4</games> 
     <points>12</points> 
     </team> 
     <team> 
     <name>chelsea</name> 
     <games>4</games> 
     <points>6</points> 
     </team> 
     <name>arsenal</name> 
     <games>5</games> 
     <points>8</points> 

    </result> 

使用PHP我想补充点( 12和8)阿森纳.....或是否有更好的方式,而不是添加它们,因为我把它们放在xml文件中我可以替换阿森纳并更新细节,比如游戏和点数,

回答

1

I可能会这样做:

<?php 
$string = <<<XML 
<result> 
    <team> 
     <name>arsenal</name> 
     <games>4</games> 
     <points>12</points> 
    </team> 
    <team> 
     <name>chelsea</name> 
     <games>4</games> 
     <points>6</points> 
    </team> 
    <team> 
     <name>arsenal</name> 
     <games>5</games> 
     <points>8</points> 
    </team> 
</result> 
XML; 

$xml = new SimpleXMLElement($string); 

$result = $xml->xpath('//team//name[.= "arsenal"]/..'); 
$points = 0; 
foreach ($result as $node) { 
    $points += $node->points; 
} 
echo "SCORE = " . $points; 
+0

非常感谢你,我可以问另一个问题,我在桌上展示我的结果,但是我想结果是(最高 - 最低)顺序,任何想法? PS。非常感谢上一个问题,它对我有很大的帮助 – 2012-03-03 06:59:30