我有一个xml聊天脚本,我在其中通过php添加新聊天。但与此同时,我希望能够更新xml中的特定用户徽章,但根本无法弄清楚如何?
这是我的xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<players>
<player id="1">4</player>
<player id="2">3</player>
</players>
<chats>
<chat playerid="2">Some chat here...</chat>
<chat playerid="1">skfgjh kjgh fdskgjhdf kgjhdf gkjd gkjdfhg dkfjhg</chat>
</chats>
</data>
这是我在其中添加新聊天的php:
$file = 'chat.xml';
$fp = fopen($file, "rb") or die("cannot open file");
$str = fread($fp, filesize($file));
$xml = new DOMDocument("1.0", "ISO-8859-1");
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML($str) or die("Error");
$library = $xml->documentElement;
$message = $xml->createElement("chat","sdlkj oghdfiguh fdiugh eiuh eriu irgh eigu ekgjher");
$messageAttribute = $xml->createAttribute('playerid');
$messageAttribute->value = $opponent;
$message->appendChild($messageAttribute);
$library->childNodes->item(1)->appendChild($message);
echo "<xmp>". $xml->saveXML() ."</xmp>";
我试图添加这个,但没有运气:
$xmlPath = new DOMXPath($xml);
$xmlNode = $xmlPath->query('//player[id="1"]');
我想将4更改为0。
有人可以帮忙: - /
答案 0 :(得分:0)
$xmlNode = $xml->xpath('//player[@id = "1"]');