phpxmlcakephpdomdocumentcakephp-2.2

CakePHP Xml utility library triggers DOMDocument warning


I'm generating XML in a view with CakePHP's Xml core library:

$xml = Xml::build($data, array('return' => 'domdocument'));
echo $xml->saveXML();

View is fed from the controller with an array:

$this->set(
    array(
        'data' => array(
            'root' => array(
                array(
                    '@id' => 'A & B: OK',
                    'name' => 'C & D: OK',
                    'sub1' => array(
                        '@id' => 'E & F: OK',
                        'name' => 'G & H: OK',
                        'sub2' => array(
                            array(
                                '@id' => 'I & J: OK',
                                'name' => 'K & L: OK',
                                'sub3' => array(
                                    '@id' => 'M & N: OK',
                                    'name' => 'O & P: OK',
                                    'sub4' => array(
                                        '@id' => 'Q & R: OK',
                                        '@'   => 'S & T: ERROR',
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    )
);

For whatever the reason, CakePHP is issuing an internal call like this:

$dom = new DOMDocument;
$key = 'sub4';
$childValue = 'S & T: ERROR';
$dom->createElement($key, $childValue);

... which triggers a PHP warning:

Warning (2): DOMDocument::createElement(): unterminated entity reference               T [CORE\Cake\Utility\Xml.php, line 292

... because (as documented), DOMDocument::createElement does not escape values. However, it only does it in certain nodes, as the test case illustrates.

Am I doing something wrong or I just hit a bug in CakePHP?


Solution

  • The problem seems to be in nodes that have both attributes and values thus need to use the @ syntax:

    '@id' => 'A & B: OK',  // <-- Handled as plain text
    'name' => 'C & D: OK', // <-- Handled as plain text
    '@' => 'S & T: ERROR', // <-- Handled as raw XML
    

    I've written a little helper function:

    protected function escapeXmlValue($value){
        return is_null($value) ? null : htmlspecialchars($value, ENT_XML1, 'UTF-8');
    }
    

    ... and take care of calling it manually when I create the array:

    '@id' => 'A & B: OK',
    'name' => 'C & D: OK',
    '@' => $this->escapeXmlValue('S & T: NOW WORKS FINE'),
    

    It's hard to say if it's bug or feature since the documentation doesn't mention it.