PHP Invalid character at XML input
1I wrote up a tool that uses PEAR’s xml serializer to convert product objects into XML and save it as a file. My other supplement stores read the file and insert or update the products as necessary. I ran into problem after problem with the error “Invalid character at XML input” and then some line and character number was included, which usually pointed at nothing or white space in the file. I tried putting everything in cdata and that didn’t solve the issue. I implemented iconv to strip out all non UTF-8 characters on a couple fields like the product description, which may contain any number of crap characters. This worked for those fields and lead to the solution of either applying iconv to every text field that could possibly have a special character or apply it to the whole XML doc as a variable before it’s written. The code below did the trick.
$xml = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $xml));
Hi I'm Nick Bartlett and thanks for visiting my blog. I'm not much of a writer; many of my posts are short and to the point while others are meant to be a reference for myself and other web developers.
Hey,
Say Im using
$stats->load( $stats_xml );
can I load it into a string $xml so
$xml = htmlentities(iconv(‘UTF-8′, ‘UTF-8//IGNORE’, $xml));
will work?
thanks