PHP Invalid character at XML input

1

Posted by nick | Posted in Uncategorized | Posted on Apr 24 2009

I 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));
  • Share/Bookmark

Comments (1)

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

Write a comment