Sunday, April 27, 2014

SimpleXML error - unterminated entity reference.

While repurposing a blog today into XML doc, ran into the usual ampersand error. Warning
: SimpleXMLElement::addChild(): unterminated entity reference.

The error is a result of addchild inside SimpleXML, which does not automatically escapes ampersand. The best way to fix this problem is to simply not use the addchild method but instead of going direct:

// change the following:
// $childnode = $xml->addChild( $key, $value, 'url' );

// into this:
$xml->addChild( $key );
$xml->$key = $value;

No comments: