PHP Doku:: Kompletten CDATA-Block schreiben - function.xmlwriter-write-cdata.html

Verlauf / Chronik / History: (4) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationXMLWriterXMLWriter FunktionenXMLWriter->writeCData

Ein Service von Reinhard Neidl - Webprogrammierung.

XMLWriter Funktionen

<<XMLWriter->writeAttribute

XMLWriter->writeComment>>

XMLWriter->writeCData

(PHP 5 >= 5.1.2, PECL xmlwriter >= 0.1.0)

XMLWriter->writeCDataKompletten CDATA-Block schreiben

Beschreibung

Objektorientiert:

bool XMLWriter::writeCData ( string $content )

Prozedural:

bool xmlwriter_write_cdata ( resource $xmlwriter , string $content )

Schreibt einen vollständigen CDATA-Block.

Parameter-Liste

xmlwriter

Nur für prozedurale Aufrufe. Die XMLWriter-resource, die bearbeitet werden soll. Diese Ressource wird von Aufrufen von xmlwriter_open_uri() oder xmlwriter_open_memory() geliefert.

content

Inhalt des CDATA-Blocks.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.


4 BenutzerBeiträge:
- Beiträge aktualisieren...
t dot w dot natt at UNIVERSITY dot bath dot ac dot uk
9.09.2008 17:18
Seen a couple of bug reports and gotchas with this one but not a simple code example. Just in case someone finds it useful:

<?php

// serve xml doc as xml
header('Content-type: application/xml');

// set up the document
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('mydoc');
$xml->startElement('myele');

// CData output
$xml->startElement('mycdataelement');
$xml->writeCData("text for inclusion within CData tags");
$xml->endElement();

// end the document and output
$xml->endElement();
$xml->endElement();
echo
$xml->outputMemory(true);
?>

Outputs:
<?xml version="1.0" encoding="UTF-8"?>
<mydoc>
    <myele>
        <mycdataelement><![CDATA[text for inclusion within CData tags]]>
        </mycdataelement>
    </myele>
</mydoc>
harvey dot robin at gmail dot com
8.09.2007 13:57
This function will not have any effect if the last node you output was a text node.  This includes whitespace.  Example:

<?php
/*
<html>
<![CDATA[ Summut nice ]]>
<p>something</p>
</html>
*/
$xw = new XMLWriter;
$xw->openMemory();
$xw->startDocument('1.0', 'ISO-8859-1');
$xw->startElement('html');
$xw->text('Comment out this line to get the CDATA back!!');
$xw->writeCData('Summut nice');
$xw->writeElement('p', 'something');
$xw->endElement();
$xw->endDocument();
echo
$xw->outputMemory();
?>
thesoupdragon at hotmail dot com
21.06.2007 11:14
A rather strange effect with this.

UTF8 Mysql database

                            $xml->startElement('tablecolor2');
    $xml->writeCData( $tablecolor2 );
    $xml->endElement();

does not work !

but
                            $xml->startElement('tablecolor2');
    $tablecolor2 = utf8_decode ( $val['pcolor2']);
    $xml->writeCData( utf8_encode ($tablecolor2) );
    $xml->endElement();

cannot explain this - but it may help someone
dave at dtracorp dot com
6.03.2007 10:30
i don't know if this is a bug with the underlying c code or not, or this is by design (i'm not a big xml guy), but for me, running on fedora core 5, php 5.2.1
to get this to work, the cdata functions need to wrap the description (or whatever element the cdata should appear in)
for example.
// initiate xmlwriter object as $xw
// add header, titles, etc.

// start cdata
$xw->startCData();
// start description
$xw->startElement('description');
// write cdata
$xw->writeCData('<img src="http://php.net/images/php.gif" />');
// write the description contents
$xw->text('php logo');
// end the description element
$xw->endElement();
// end the cdata
$xw->endCData();

// end xml, and output

otherwise, i just got warnings about writing cdata in the wrong context, and no cdata would be written



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)