SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    html in CDATA problem

    hi,

    I have the following xml

    Code XML:
    <?xml version="1.0" encoding="UTF-8"?>
     
    <homePageContent>
     
    <content>
    <![CDATA[
    <h2>Main Content</h2>
    <p>some paragraph here</p>
    <p>another paragraph here</p>
    ]]>
    </content>
     
    </homePageContent>

    and I'm using this to edit the xml:

    Code PHP:
    function setHomePageContent($newContent, $xmlFile) {
    	$xml = new DomDocument('1.0', 'UTF-8');
    	$xml->load($xmlFile);
    	$root = $xml->documentElement;
    	$children = $root->childNodes;
    	foreach ($children as $element) {
    		if ($element->nodeType == XML_ELEMENT_NODE && $element->nodeName == 'content') {
    			$element->nodeValue = '<![CDATA[ ' . $newContent . ' ]]>';
    		}
    	}
    	if ($xml->save($xmlFile)) { 	
    		return true;
    	}
    }

    but the special characters like "<" are presented like &lt; in my updated xml.
    I don't need this, I need my "<" character. Can anyone help me with this?

    10x

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Running the string through html_entity_decode() should do the trick.

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daniel0 View Post
    Running the string through html_entity_decode() should do the trick.
    Daniel0, 10x for replying me

    Sorry, but don't work properly. I've save the $newContent to ".txt" files before and after html_entity_decode(), something like this:

    Code PHP:
    function setHomePageContent($newContent, $xmlFile) {
        $xml = new DomDocument('1.0', 'UTF-8');
        $xml->load($xmlFile);
        $root = $xml->documentElement;
        $children = $root->childNodes;
        foreach ($children as $element) {
            if ($element->nodeType == XML_ELEMENT_NODE && $element->nodeName == 'content') {
     
            	$fp = fopen('test.txt', w);
    			fwrite($fp, $newContent);
    			fclose($fp);
     
            	$tmpContent = html_entity_decode($newContent);
     
    			$fp = fopen('test2.txt', w);
    			fwrite($fp, $tmpContent);
    			fclose($fp);
     
                $element->nodeValue = "\n" . '<![CDATA[ ' . $tmpContent . ' ]]>' . "\n";
            }
        }
        if ($xml->save($xmlFile)) {
            return true;
        }
    }

    but the content of "test.txt" and "test2.txt" matches exactly. I mean there is no &lt; characters in my variable before put it in my xml.
    any other ideas ?

    10x


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •