2012-03-02 44 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