hi every one i have an xml file and want to update my code is following
<gallerylist>
<record>
<movie>videos/15Avatar_HD.flv</movie>
<title>Avatar1</title>
<desc>It plays any kind of video file that Flash Player supports</desc>
<preview>videos/previews/6avatar.jpg</preview>
<imgplaylist>videos/imgplaylist/14p1.jpg</imgplaylist>
<category>Action</category>
</record>
</gallerylist>
and my php function is following:
function xml_update_video($id,$searchString)
{
$res=$this->selectVideo($id);
$searchString = $searchString;
$doc = new DOMDocument;
$doc->formatOutput = TRUE;
$doc->preserveWhiteSpace = FALSE;
$doc->load('../playlist.xml');
$xPath = new DOMXPath($doc);
$query = sprintf('//record[./title[text()="%s"]]', $searchString);
foreach($xPath->query($query) as $node)
{
$node->parentNode->removeChild($node);
}
$gallerylist = $doc->getElementsByTagName( "gallerylist" )->item(0);
$record = $gallerylist->appendChild($doc->createElement('record'));
$movie =开发者_高级运维 $record->appendChild($doc->createElement('movie'));
$movie->appendChild($doc->createTextNode('videos/'.$res['videofile']));
$title = $record->appendChild($doc->createElement('title'));
$title->appendChild($doc->createTextNode($res['title']));
$desc = $record->appendChild($doc->createElement('desc'));
$desc->appendChild($doc->createTextNode($res['description']));
$preview = $record->appendChild($doc->createElement('preview'));
$preview->appendChild($doc->createTextNode('videos/previews/'.$res['previewImage']));
$imgplaylist = $record->appendChild($doc->createElement('imgplaylist'));
$imgplaylist->appendChild($doc->createTextNode('videos/imgplaylist/'.$res['playlistImage']));
$category = $record->appendChild($doc->createElement('category'));
$category->appendChild($doc->createTextNode($res['category']));
$doc->formatOutput = true;
$test1 = $doc->saveXML();
$doc->save('../playlist.xml');
}
$id is id of video file in database . with help of id i am adding new node in xml file after deleting node on basis of searchstring which is title.This code is not working properly. it add new node.
this file add new node .which is not right. this should be update existing node. but how ? can anyone help
Consider what you said it should do
this file add new node .which is not right. this should be update existing node.
against what you are actually doing
with help of id i am adding new node in xml file after deleting node on basis of searchstring which is title.This code is not working properly. it add new node.
Of course it adds a new node. Look at your code. First you remove the found node and then you are creating and appending new nodes. If you want to change the data of the found node, do not delete it but work with it's properties, e.g. with the nodeValue
but how ?
Sorry, I am too lazy, busy and tired to write yet another lengthy DOM example, so please bear with me if I only link you to some of my previous answers about DOM and XML.
精彩评论