FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Adding a CDATA section
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Adding a CDATA section [message #173573 is a reply to message #173555] Tue, 19 April 2011 07:40 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 18/04/2011 21:33, JustWondering escribió/wrote:
> I am trying to add a CDATA section to a DOMdocument.
>
> Code:
>
> <?php
> // Create a DOMDocument object and set nice formatting
> $doc = new DOMDocument( "1.0", "UTF-8" );
> $doc->formatOutput = true;
>
> // Create the root "stockList" element
> $stockList = $doc->createElement( "stockList" );
> $doc->appendChild( $stockList );
>
> // Create the first "item" element (apple)
> $item = $doc->createElement( "item" );
> $item->setAttribute( "type", "fruit" );
> $stockList->appendChild( $item );
>
> // Create the item's "description" child element
> $description = $doc->createElement( "description" );
> $item->appendChild( $description );
> $cdata = $doc->createCDATASection( "Apples are<b>yummy</b>" );
> $description->appendChild( $cdata );
>
> // Create the second "item" element (beetroot)
> $item = $doc->createElement( "item" );
> $item->setAttribute( "type", "vegetable" );
> $stockList->appendChild( $item );
>
> echo $doc->saveXML();
> ?>
>
> What I'm expecting to see is Apples are yummy, with yummy in bold.
> Here's what I get:
>
> yummy]]>

You are getting the expected output, you are just not viewing it
properly. You need to add the appropriate Content-Type header so the
browser knows it is an XML document:

header('Content-Type: text/xml; charset=UTF-8');

Otherwise, the server will send the default type, which is probably
text/html.


> Firebug / Edit gives me this:
>
> <stocklist>
> <item type="fruit">
> <description><!--[CDATA[Apples are<b-->yummy]]&gt;</description>
> </item>
> <item type="vegetable">
> </item></stocklist>

That panel does not display the original source code but and an
interpretation of it. In this case, the code misinterpreted as HTML and
fixed to fit.

Use the browser's "View source" feature instead (or run the script from
the command line).


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Using exec with & - what might I get back?
Next Topic: php sessions and css file?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 17:38:21 GMT 2024

Total time taken to generate the page: 0.04411 seconds