Search code examples
phpxmlsimplexmlcdata

simplexml editing CDATA node


I have an xml file, I want to open it, edit certain CDATA node with the values from $_POST input and save it as same file, I've read some online documentation and ended up here, someone please suggest a nice way of doing this...

regardsh


Solution

  • SimpleXML does not make CDATA elements accessible by default. You can either tell simplexml to skip them (default) or to read them (see: read cdata from a rss feed). If you read them, they are standard text values, so they get merged with other textnodes.

    More control is offered by the Document Object ModelDocs, which offers a DOMCdataSection which extends from DOMText, the standard text node model.

    Even though this is a different PHP library (DOM vs. SimpleXML), both are compatible to each other. For example a SimpleXMLElement can be converted into a DOMElement by using the dom_import_simplexml function.

    If you post some code what you've done so far it should be easy to figure out how to access the CDATA sections you want to modify. Please provide as well some demo XML data so the example is more speaking.