2011-08-16 59 views
2

我的XML(hashes.xml)是的SimpleXML不會增加子女或屬性爲xml

<?xml version="1.0" encoding="UTF-8" ?> 
<root> 
    <updated></updated> 
    <players> 
     <playernum>1</playernum> 
     <!--Demonstration of syntax, using the value 127.0.0.1 and the first second of December 22, 2012 with salt of 000 --> 
     <player id="7bea7450391c9d89c65af7a46966e45066105fa4"> 
      <game id="1"> 
       <color>red</color> 
      </game> 
      <game id="2"> 
       <color>purple</color> 
      </game> 
     </player> 
    </players> 
</root> 

我的PHP是

<?php 

$hash = $_GET["hash"]; 

$xml = simplexml_load_file("hashes.xml"); 
$xml->players->addChild("player", " "); 

?> 

但是當我運行PHP時,不更改xml文件。我願意使用任何方法,即使它不是simplexml。

+0

你實際上是否用類似'file_put_contents('hashes.xml',$ xml-> asXml())''的方式將XML保存回去? – prodigitalson

+0

我沒有。這可能是我的問題。編輯:不。還是行不通。 – quadthagoras

回答

0

您已有player孩子在您的players。 我想你players$xml->players->addChild("player2", " ");

編輯需要另一個名字:我測試了,我看到我錯了

這種實際工作

$xml = '<?xml version="1.0" encoding="UTF-8" ?> 
<root> 
    <updated></updated> 
    <players> 
     <playernum>1</playernum> 
     <!--Demonstration of syntax, using the value 127.0.0.1 and the first second of December 22, 2012 with salt of 000 --> 
     <player id="7bea7450391c9d89c65af7a46966e45066105fa4"> 
      <game id="1"> 
       <color>red</color> 
      </game> 
      <game id="2"> 
       <color>purple</color> 
      </game> 
     </player> 
    </players> 
</root>'; 


$xml = simplexml_load_string($xml); 
$newPlayer = $xml->players->addChild("player", " "); 
$newPlayer->addAttribute("id", "12313123123"); 
$newGame = $newPlayer->addChild("game", ""); 
$newGame->addAttribute("id", "1"); 
$newColor = $newGame->addChild("color", "blue"); 
echo $xml->asXML(); 

結果代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <updated/> 
    <players> 
     <playernum>1</playernum> 
     <!--Demonstration of syntax, using the value 127.0.0.1 and the first second of December 22, 2012 with salt of 000 --> 
     <player id="7bea7450391c9d89c65af7a46966e45066105fa4"> 
      <game id="1"> 
       <color>red</color> 
      </game> 
      <game id="2"> 
       <color>purple</color> 
      </game> 
     </player> 
     <player id="12313123123"> 
      <game id="1"> 
       <color>blue</color> 
      </game> 
     </player> 
    </players> 
</root> 
+0

但即使當我改變「echo $ xml-> asXML();」到「$ xml-> asXML(」hashes.xml「);」更改不會顯示在hashes.xml中 – quadthagoras

+0

日誌文件說明權限被拒絕,因此無法打開該流。 – quadthagoras

+0

我剛剛與file_put_contents一起工作 – quadthagoras