0

Possible Duplicate:
Grabbing the href attribute of an A element

I have an xml chat script in which I add new chats via php. But at the same time I want to be able to update a specific users badge in the xml, but simply can't figure out how?

Here is my 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>

And here is the php in which I add a new chat:

$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>";

I have tried to add this but with no luck:

$xmlPath = new DOMXPath($xml);
$xmlNode = $xmlPath->query('//player[id="1"]');

I want to change the 4 to say 0.

Can somebody please help :-/

Community
  • 1
  • 1
Mansa
  • 2,277
  • 10
  • 37
  • 67

1 Answers1

0
$xmlNode = $xml->xpath('//player[@id = "1"]');             
Mike
  • 1,791
  • 13
  • 13
  • Oh yes... But how do I update this? – Mansa Sep 05 '12 at 21:48
  • $xmlNode[0] = 0; maybe? or wrap the value in another tag $xmlNode[0]->score = 0; – Mike Sep 05 '12 at 21:52
  • Now I have tried this but with no luck: `code`$library = $xml->documentElement; $xmlPath = new DOMXPath($xml); $xmlNode = $xmlPath->query('//player[@id="1"]'); $library->$xmlNode->replaceChild("0"); – Mansa Sep 05 '12 at 21:58