Encoding error when saving XML document with PHP (HTML entities)

I am creating XML file with PHP, this is the output I get

error on line 2 at column 165: Encoding error
Below is a rendering of the page up to the first error.

In the source I see only this

<?xml version="1.0" encoding="UTF-8"?>
<ad_list><ad_item class="class"><description_raw>Lorem ipsum dolor sit amet 




This is the code I am using (strip_shortcodes is WordPress function for removing tags that WordPress uses).

$xml = new DOMDocument('1.0', 'UTF-8'); 
$xml__item = $xml->createElement("ad_list");    

$ad_item = $xml->createElement("ad_item");
$xml__item->appendChild( $ad_item );

$description_raw = $xml->createElement("description_raw", strip_shortcodes(html_entity_decode(get_the_content())));
$ad_item->appendChild( $description_raw );

$xml->appendChild( $xml__item );
$xml->save($filename); 

If I remove html_entity_decode function from description_raw than full XML is generated but then I have this error

error on line 6 at column 7: Entity 'nbsp' not defined
Below is a rendering of the page up to the first error.

Read more here: Encoding error when saving XML document with PHP (HTML entities)

Leave a Reply

Your email address will not be published. Required fields are marked *