Very, very lost here. I've created some SimpleXML using 'new SimpleXMLElement' and added a few child nodes using 'addChild'. I saved it to an XML document on my server, but it was one long string. To fix this, I imported the simpleXML over to DOM. The code is

$doc = new DOMDocument('1.0');

$doc->formatOutput = true;
$domnode = dom_import_simplexml($xml);
$domnode = $doc->importNode($domnode, true);
$domnode = $doc->appendChild($domnode);
$doc->save("xxx.xml");

Now my XML looks great but I need to add some CDATA tags. I'm trying to access the nodes that need CDATA tags and use 'createCDATASection' to add them in. However, I keep getting errors around not be able to convert the element to a string. My final XML structure is below. I'm trying to add CDATA tags for the Description nodes.

XML

<TW>
<Item>
<User></User>
<Description></Description>
<Location></Location>
<Time></Time>
</Item>
</TW>
<Item>
<User></User>
<Description></Description>
<Location></Location>
<Time></Time>
</Item>
</TW>

    And how are you attempting to use createCDATASection()? What is this "element that you're not being able to convert to a string"?

    $domelement->appendChild($domdocument->createCDATASection('blahblah<fish&chips>blah'));

      Thanks for your help! I've gotten to the point where I can add cdata tags to new node elements, but not to existing ones. In my XML now, I want to replace:

      <description>xxxyyy</description>

      with

      <description><![CDATA[xxxyyy]]></description>

      I have code that loops through the XML to pull out all the description nodes, but the best I've been able to do is:

      <description>xxxyyy<![CDATA[xxxyyy]]></description>

      OR

      <description>xxxyyy</description>
      <![CDATA[xxxyyy]]>

      My problem here is that I'm using appendChild and I think I need to use replaceChild. replaceChild gives me errors though, so.....thoughts?

        Without knowing anything about your code or the errors you're getting ... the only relevant thought I have is "replaceChild sounds about right".

          Here is the issue in the most basic form.

          1. Import XML using DOM.
          2. Locate node that needs CDATA tags.
          3. Add CDATA tags.
          4. Save XML.

            To which I can only reply

            1. ....
            2. Profit!

            because it gives me no information about your code or the errors you're getting.

              I hear you. Thanks for trying to help! The errors were all over the place, depending on the solution I was trying at the time. Sorry for not being more descriptive. I finally came up with a hack which is to get the value of the node, remove it using removeChild, and add it back in using a combination of CreateElement, createCDATA, and appendChild. It's ugly, but it will do for now. Thanks again.

                Write a Reply...