Jump to content

PHP XML DOM help


vytas

Recommended Posts

Ok i have this PHP file that loads the info from a XML file then checks if a childNode of the XML document has the nodeValue Vytas , but then i run into a problem i need to have the sibling of the that node this is how the XML file looks like.

<coords>	<player>		<x>3</x>		<y>6</y>		<playername>Vytas</playername>	</player>		<player>		<x>6</x>		<y>3</y>		<playername>Gerald</playername>	</player></coords>

and this is how the PHP code looks like.

<?php$x = $_GET['x'];$y = $_GET['y'];$player = $_GET['player'];$xmldoc = new DOMDocument();$xmldoc->load("coords.xml");$x = xmldoc->documentElement;foreach ($x->childNodes AS $item) { if ($item->nodeValue == "Vytas") {  // quite helpless here. }}print $xmldoc->saveXML();?>

Im pretty new to PHP with XML so i'd really like some help

Link to comment
Share on other sites

Your script only looks at the following elements:* <player>* <player>Those are the only direct children of the documentElement.The nodeValue returned by each of those player elements is (according to tests I've done in PHP before with other XML scripts):* 36Vytas* 63GeraldYou'll have to go down one more level in order to get the nodes you want.

Link to comment
Share on other sites

You're using $x quite too many times, wouldn't you say :) :

$x = $_GET['x'];$x = xmldoc->documentElement;

<?php$x = $_GET['x'];$y = $_GET['y'];$player = $_GET['player'];$xmldoc = new DOMDocument();$xmldoc->load("coords.xml");$docRoot = $xmldoc->documentElement;foreach ($docRoot->documentElement->childNodes AS $item) {$playername = $item->getElementsByTagName('playername')->item(0);if ($playername->nodeValue == "Vytas") {  $siblings = $playername->parentNode->childNodes;/* Includes the x, y, and the playername Vytas. Whatever you want from this point on, loop over $siblings, skipping the last item in the list, since at least you now know it's the playername */}}print $xmldoc->saveXML();?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...