Php Simplexml赢得';t向xml添加子项或属性

Php Simplexml赢得';t向xml添加子项或属性,php,xml,simplexml,Php,Xml,Simplexml,我的xml(hashes.xml)是 1. 红色 紫色 我的php是 <?php $hash = $_GET["hash"]; $xml = simplexml_load_file("hashes.xml"); $xml->players->addChild("player", " "); ?> 但是当我运行php时,它不会对xml文件进行任何更改。我愿意使用任何方法,即使它不是simplexml。您的播放器中已经有子项。 我想你需要另一个名字来命名pla

我的xml(hashes.xml)是


1.
红色
紫色
我的php是

<?php

$hash = $_GET["hash"];

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

?>


但是当我运行php时,它不会对xml文件进行任何更改。我愿意使用任何方法,即使它不是simplexml。

您的
播放器中已经有
子项。
我想你需要另一个名字来命名
players
parent
$xml->players->addChild(“player2”,“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='1!'
1.
红色
紫色
';
$xml=simplexml\u load\u字符串($xml);
$newPlayer=$xml->players->addChild(“player”,“player”);
$newPlayer->addAttribute(“id”,“12313123123”);
$newGame=$newPlayer->addChild(“游戏”,“游戏”);
$newGame->addAttribute(“id”,“1”);
$newColor=$newGame->addChild(“颜色”、“蓝色”);
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> 

1.
红色
紫色
蓝色

您的
播放器中已经有
播放器
子项。 我想你需要另一个名字来命名
players
parent
$xml->players->addChild(“player2”,“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='1!'
1.
红色
紫色
';
$xml=simplexml\u load\u字符串($xml);
$newPlayer=$xml->players->addChild(“player”,“player”);
$newPlayer->addAttribute(“id”,“12313123123”);
$newGame=$newPlayer->addChild(“游戏”,“游戏”);
$newGame->addAttribute(“id”,“1”);
$newColor=$newGame->addChild(“颜色”、“蓝色”);
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> 

1.
红色
紫色
蓝色

您是否真的使用类似于
file\u put\u contents('hashes.XML',$XML->asXml()?我没有。那可能是我的问题。编辑:没有。仍然不起作用。您是否确实使用类似于
file\u put\u contents('hashes.XML',$XML->asXml())的soemthing将XML保存了回来?我没有。那可能是我的问题。编辑:没有。仍然不起作用。但即使我将“echo$xml->asXML();”更改为“$xml->asXML(“hashes.xml”);”更改也不会显示在hashes.xml中日志文件说权限被拒绝,因此无法打开流。但即使我将“echo$xml->asXML();”更改为“$xml->asXML(“hashes.xml”);”更改没有显示在hashes.xml中。日志文件说权限被拒绝,因此无法打开流。