Page 1 of 1
Replacing XML Node value with SimpleXML
Posted: Fri Aug 03, 2007 2:04 pm
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
Posted: Sat Aug 04, 2007 12:35 am
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';
Posted: Sat Aug 04, 2007 3:48 am
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());
Posted: Mon Aug 06, 2007 3:11 am
by jchannon
Can anyone help?
Posted: Mon Aug 06, 2007 4:42 am
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).
Posted: Mon Aug 06, 2007 8:35 am
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?
Posted: Mon Aug 06, 2007 8:51 am
by volka
jchannon wrote:Is SimpleXML better than the DOM then?
Why and how did you come to this conclusion?
Posted: Mon Aug 06, 2007 8:55 am
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?
Posted: Mon Aug 06, 2007 9:05 am
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.