如何编辑xml节点值


How to edit xml node value

我正在使用Jw播放器adtonomy插件编写一个自定义插件,用于我的网站,该插件从XML文件加载广告。但是,我不知道如何更新文件中的节点。我尝试过使用简单的xml,但不知道如何更新。任何实施都受到高度评价。谢谢这是xml文件。

<xml>
<plugins>adtimage</plugins>
<adtimage.graphic>http://mysite/default.png</adtimage.graphic>
<adtimage.link>http://targetsite.com</adtimage.link>
<adtimage.positions>pre,post</adtimage.positions>
<adtimage.onpause>true</adtimage.onpause>
<adtimage.txt>Advertisement</adtimage.txt>
<adtimage.btntxt>Click to continue with video</adtimage.btntxt>
</xml>

我的代码在这里。

<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
 echo $child->getName() . ": " . $child . "<br />";
 /*
   TO DO
   change the node value of 
   <adtimage.graphic>http://mysite/default.png</adtimage.graphic>
   to
   <adtimage.graphic>http://stackoverflow.com</adtimage.graphic>
 */
 }
?>

您可以使用此

//Load XML
$xml = simplexml_load_file("test.xml");
// Modify a node
$xml->{"adtimage.graphic"} = 'http://stackoverflow.com/adtimage.graphic';
// Saving the whole modified XML to a new filename
$xml->asXml('updated.xml');

示例输出"updated.xml"

<?xml version="1.0"?>
<xml>
<plugins>adtimage</plugins>
<adtimage.graphic>http://stackoverflow.com/adtimage.graphic</adtimage.graphic>
<adtimage.link>http://targetsite.com</adtimage.link>
<adtimage.positions>pre,post</adtimage.positions>
<adtimage.onpause>true</adtimage.onpause>
<adtimage.txt>Advertisement</adtimage.txt>
<adtimage.btntxt>Click to continue with video</adtimage.btntxt>
</xml>