By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

Problems with DOM XML

P: n/a
Hey guys
Im trying to build a plugin for a php script. Its going fine and
dandy.

Data is store in an XML file. Which has the following structure:

<raiders>
<raider>
<name>Anarchie</name>
<ready>?</ready>
</raider>
</raiders>

What i need to do is be able to find a "raider" and change the ready
node to something else.

For this i wrote this code snippet:
-

$doc = new DomDocument();
$doc->load($DataDir."/raid.bot/raid.xml");

$raiders = $doc->getElementsByTagName("raider");
foreach ($raiders as $raider)
{
$names = $raider->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
if($name = $sender)
{
$statuses = $raider->getElementsByTagName("ready");
$ready = $statuses->item(0)->setContent("yes");
}
}
$doc->save($DataDir."/raid.bot/raid.xml");
send_reply("Updated status to ready");

-
However it complains about the "set content" method:
PHP Fatal error: Call to undefined method DOMElement::setContent() in
F:\Games\
guildBot\plugins\tell\raid.bot\index.php on line 89

I have tried set_content() aswell.

Any ideas what im doing wrong here? Is set_content really the only way
to update a node in DOM xml?

Thoughts, ideas and suggestions are welcome.

- Mike
Aug 18 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Aug 18, 1:54*pm, Michael <michael.fr...@gmail.comwrote:
I have tried set_content() aswell.
Maybe this comment on the PHP site can help you:
http://nl2.php.net/manual/en/functio...tent.php#30324
Aug 18 '08 #2

P: n/a
On 18 Aug., 14:46, Sjoerd <sjoer...@gmail.comwrote:
On Aug 18, 1:54 pm, Michael <michael.fr...@gmail.comwrote:
I have tried set_content() aswell.

Maybe this comment on the PHP site can help you:http://nl2.php.net/manual/en/functio...tent.php#30324
I figured it was becouse i was using deprecated features. That
explained a lot :).

Trying to get this to work now:
send_reply("starting DOM");
$doc = new DomDocument();
$doc->load($DataDir."/raid.bot/raid.xml");

$raiders = $doc->getElementsByTagName("raider");
foreach ($raiders as $raider)
{
$names = $raider->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
if($name = $sender)
{
$newready = $doc->createElement("ready");
$newreadytext = $doc->createTextNode("yes");
$newready->appendChild($readytext);
$ready = $raider->getElementsByTagName("ready")->item(0);
$raider->replaceChild($newready,$ready);
$doc->save($DataDir."/raid.bot/raid.xml");
send_reply("Updated status to ready");
}
}

No errors and it gets as far as to get into the loop. Nothing happens
after that though.
Aug 18 '08 #3

P: n/a
On 18 Aug., 15:13, Michael <michael.fr...@gmail.comwrote:
On 18 Aug., 14:46, Sjoerd <sjoer...@gmail.comwrote:
On Aug 18, 1:54 pm, Michael <michael.fr...@gmail.comwrote:
I have tried set_content() aswell.
Maybe this comment on the PHP site can help you:http://nl2.php.net/manual/en/functio...tent.php#30324

I figured it was becouse i was using deprecated features. That
explained a lot :).

Trying to get this to work now:
send_reply("starting DOM");
$doc = new DomDocument();
$doc->load($DataDir."/raid.bot/raid.xml");

$raiders = $doc->getElementsByTagName("raider");
foreach ($raiders as $raider)
{
$names = $raider->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
if($name = $sender)
{
$newready = $doc->createElement("ready");
$newreadytext = $doc->createTextNode("yes");
$newready->appendChild($readytext);
$ready = $raider->getElementsByTagName("ready")->item(0);
$raider->replaceChild($newready,$ready);
$doc->save($DataDir."/raid.bot/raid.xml");
send_reply("Updated status to ready");
}

}

No errors and it gets as far as to get into the loop. Nothing happens
after that though.
i solved it.

$parent = new DomDocument;
$parent_node = $parent ->createElement('raider');
$parent_node->appendChild($parent->createElement('name', $sender));
$parent_node->appendChild($parent->createElement('ready', 1));
$parent->appendChild($parent_node);

$dom = new DomDocument;
$dom->load($DataDir."/raid.bot/raid.xml");

$xpath = new DOMXpath($dom);
$nodelist = $xpath->query('/raiders/raider');
$i = 0;
foreach($nodelist as $nodes)
{
$names = $nodes->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
$i++;
if($name == $sender)
{
break;
}
}
$oldnode = $nodelist->item($i-1); //Dirty hack
$newnode = $dom->importNode($parent->documentElement, true);
$oldnode->parentNode->replaceChild($newnode, $oldnode);
$dom->save($DataDir."/raid.bot/raid.xml");
Aug 18 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.