sammyukavi
sammyukavi

Reputation: 1511

How to edit xml node value

I'm writing a custom plugin for use in my website using Jw player adtonomy plugin which loads adverts from XML files. However I do not know how to update the nodes in the files. I have tried using simple xml but I don't know how to update. Any implementation is highly regarded. Thanks. Here is the xml file.

<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>

my code is here.

<?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>
 */
 }
?>

Upvotes: 2

Views: 149

Answers (1)

Baba
Baba

Reputation: 95161

You can use this

//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');

Example Output '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>

Upvotes: 1

Related Questions