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


i creating xml file php, output

error on line 2 @ column 165: encoding error below rendering of page first error. 

in source see this

<?xml version="1.0" encoding="utf-8"?> <ad_list><ad_item class="class"><description_raw>lorem ipsum dolor sit amet &#13; &#13; &#13; &#13; 

this code using (strip_shortcodes wordpress function removing tags 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 remove html_entity_decode function description_raw full xml generated have error

error on line 6 @ column 7: entity 'nbsp' not defined below rendering of page first error. 

it's highly unlikely value you're getting going valid in xml document. should adding cdata section. try this:

<?php $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"); $cdata = $xml->createcdatasection(get_the_content()); $description_raw->appendchild($cdata); $ad_item->appendchild( $description_raw );  $xml->appendchild( $xml__item ); $xml->save($filename); 

alternatively, build yourself:

<?php $content = get_the_content(); $xml = <<< xml <?xml version="1.0"?> <ad_list>     <ad_item>         <description_raw><![cdata[ $content ]]></description_raw>     </ad_item> </ad_list>  xml; file_put_contents($filename, $xml); 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -