Re: Adding a CDATA section [message #173573 is a reply to message #173555] |
Tue, 19 April 2011 07:40 |
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]]></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
--
|
|
|