Avatar of Kobz46
Kobz46
 asked on

How to handle special characters with XMLReader in PHP

I am having problems parsing an XML file which contains special characters. I am using the built in XMLreader in PHP. All XML records are processed fine, until one of the values contain a special character, then I get the following error:

Warning: XMLReader::read(): An Error Occured while reading in...

Once I remove the character it parses fine, but I need to be able to parse special characters and write them into my database.
$description = "";  $search_text = "";
$reader = new XMLReader();
$reader = XMLReader::open("data_import.xml") or die("XMLReader::open - Cannot read file"); 
while ($reader->read())
{
	if ($reader->nodeType == XMLREADER::ELEMENT && !$reader->isEmptyElement) 
	{
		if($reader->name == "description")
		{
			$reader->read();
			$description = "DESC: ".$reader->value."\n\n";
			$reader->read();
		}
		
		if($reader->name == "search_text")
		{
			$reader->read();
			$search_text = "SEARCH: ".$reader->value;
			$reader->read();
		}
	}
}
echo $description."\n\n".$search_text;
echo "\n\nDONE \n\n";

Open in new window

ProgrammingPHPXML

Avatar of undefined
Last Comment
pkoops

8/22/2022 - Mon
pkoops

You need to be sure which encoding is used by your PHP-Interpreter and the encoding used in the xml-file.
Make sure, that both points are using the same (recommended UTF-8), then it should work.

http://www.php.net/setlocale
Kobz46

ASKER
This is my XML header:  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
When I open the XML file using Firefox, the special characters show as "black diamond with white question mark". When I use "us-ascii" as my encoding it shows up correctly in firefox. So which encoding should I use?
You said it should work when I make both UTF-8. If my XML encoding is left like above, how do I make sure my PHP XMLreader is interpreting it in UTF8?
I had a look at the setlocale, but can't get it working.
See my test XML file attached. I am trying to parse it with the code in my initial post.
 
 

data-import.xml.txt
ASKER CERTIFIED SOLUTION
pkoops

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61