2012-08-02 77 views
2

我正在使用Jw player adtonomy插件从XML文件加载广告,在我的网站上使用自定义插件。但是我不知道如何更新文件中的节点。我曾尝试使用简单的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> 
*/ 
} 
?> 

回答

1

您可以使用此

//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> 
+0

感谢。它工作的时间很长。 – sammyukavi 2012-08-02 10:10:54

+0

欢迎您 – Baba 2012-08-02 10:40:00