SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: xmlwriter writecdata blank

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xmlwriter writecdata blank

    hi all

    i'm using xmlwriter to write rss feeds, but i'm having an issue with the writecdata function
    i'm using it in this context
    PHP Code:
    // create the xmlwriter object, etc.

    $xw->startElement('description');
    $xw->startCData();
    $xw->writeCData('<img src="' $domain $obj[$i]->getImage('t') . '" />');
    $xw->endCData();
    $xw->text($obj[$i]->getText());
    $xw->endElement();

    // close the xmlwriter object, and output 
    but all i'm getting in the output is something like

    <description>
    <!CDATA[[]]>all the other text
    </description>

    when it should look like

    <description>
    <!CDATA[[<img src="http://dtracorp.com/print/image/filename" />]]>all the other text
    </description>

    setting the error_reporting to E_All i get this
    PHP Warning: XMLWriter::writeCdata(): xmlTextWriterStartCDATA : CDATA not allowed in this context!

    when i put the cdata call after the text() call, no errors, but no cdata at all

    anyone know how to use this properly?

    thanks
    dave

  2. #2
    SitePoint Member
    Join Date
    May 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case someone finds this, and having the same issue.

    This user should have done the code differently, he specified the creation of a CData node, and then tried creating another inside the existing. This example is trivial, but hopefully shows what went wrong.

    His example implied he wanted data wrapped in CData, and something extra left over. Who truly knows, but I'm giving the correct syntax anyway.

    PHP Code:
    // create the xmlwriter object, etc.
    $xw->startElement('description');
    // First example, create the node along with the data.
    $xw->writeCData('data inside the CData');

    // Second example, create the node and also add data, not forgetting to close it.
    $xw->startCData();
    $xw->text('fill text');
    $xw->endCData();

    $xw->endElement(); // End description

    // close the xmlwriter object, and output 
    End result of running that would be
    Code:
    <description><![CDATA[data inside the CData]]>fill text</description>

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
  •