Replacing XML Node value with SimpleXML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Replacing XML Node value with SimpleXML

Post by jchannon »

I have got some code that returns my node from an XML value.

Code: Select all

$xml = simplexml_load_file('books.xml');
$file = "books.xml";
$bodyxpath = "//page[@id='".$pageid."']/body";
$result = $xml->xpath($bodyxpath);
$body = $result[0];
I have read you can change that value by doing

Code: Select all

$result[0]="test";
echo $result[0];
I have also read to replace that back into the XML file you call

Code: Select all

file_put_contents($file, $xml->asXML());
However I cannot get this to work at all. In my testing, I have managed to get it to write back into the XML file in all sorts of ways but all I want to do is to replace the existing content in that node and also preferable within a <![CDATA[]]> tag. Ideally I am using the SimpleXML approach rather than the DOM methods.

Can someone please advise where I am going wrong.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$xml = simplexml_load_file('books.xml');
$file = "books.xml";
$bodyxpath = "//page[@id='".$pageid."']";
$result = $xml->xpath($bodyxpath);
$result[0]->body = 'test';
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Post by jchannon »

Thank You, Thank You, Thank You. Thats been driving me nuts.

There is one thing though. I have tried putting "test" in a CData String but when it writes it to the XML file, it doesnt appear to be a proper CData section. Any ideas? This is what I have

Code: Select all

$bodyxpath = "//page[@id='".$pageid."']";
$result = $xml->xpath($bodyxpath);
$result[0]->bodycontent = '<![CDATA[<h1>test</h1>]]>';
file_put_contents($file, $xml->asXML());
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Post by jchannon »

Can anyone help?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I don't think simplexml can do that. But the DOM extension can. Both use the same underlying library and the models are interchangeable (dom_import_simplexml / simplexml_import_dom).
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Post by jchannon »

I have fixed it by using the htmlentities() method and then use html_entity_decode() when reading it.

Is SimpleXML better than the DOM then?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jchannon wrote:Is SimpleXML better than the DOM then?
Why and how did you come to this conclusion?
jchannon
Forum Newbie
Posts: 12
Joined: Fri Aug 03, 2007 1:55 pm

Post by jchannon »

Sorry my question was the wrong way around. After my research, it came clear that people preferred using SimpleXML. However as you can tell I have found it quite restrictive in terms of functionailty. So I just wondered if the DOM was better? If so why was SimpleXML released?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/simplexml wrote:The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
http://devzone.zend.com/node/view/id/688 wrote:PHP5's new XML support was designed as a coherent set of APIs to process and manipulate XML. This includes the DOM extension, which provides all you'll ever need for handling XML, the SAX API for streaming XML parsing, XSLT for XML transformations and SimpleXML when you need to do anything else.
If the functionality of SimpleXML suffices you might find it easier to use the simple interface to access the data stored in a xml document. If not, you need something else e.g. the DOM extension.
Post Reply