How do I rename XML values using php? -
how rename value in xml using php? i've got far:
<?php $q = $_get["q"]; $q = stripslashes($q); $q = explode('|^', $q); $old = $q[0]; $dom = new domdocument; $dom->preservewhitespace = false; $dom->formatoutput = true; $dom->load("test.xml"); $xpath = new domxpath($dom); $query1 = 'channel/item[title="' . $old . '"]/title'; $entries = $xpath->query($query1); foreach ($entries $entry) { $oldchapter = $entry->parentnode->removechild($entry); $item = $dom->getelementsbytagname('item'); foreach ($item $items) { $title = $dom->createelement('title', $q[1]); $items->appendchild($title); } } $dom->save("test.xml");
basically, take 2 titles url, old existing title, , 1 user wants change (so oldtitle|^newtitle
), , puts them array.
what i've tried doing removing existing old title, , making new title with, using new title value url, doesn't seem working. going wrong, or there easier way of doing this?
the way domnode::replacechild()
. majority of code correct, you've over-complicated of dom stuff.
try this:
<?php $q = $_get["q"]; $q = stripslashes($q); $q = explode('|^', $q); $old = $q[0]; $dom = new domdocument; // *before* loading document $dom->preservewhitespace = false; $dom->formatoutput = true; $dom->load("test.xml"); $xpath = new domxpath($dom); $query1 = 'channel/item[title="' . $old . '"]/title'; $entries = $xpath->query($query1); // need in loop foreach ($entries $oldtitle) { $newtitle = $dom->createelement('title', $q[1]); $entry->parentnode->replacechild($newtitle, $oldtitle); } $dom->save("test.xml");
Comments
Post a Comment