2

I'm trying to parse an xml file that contains accents, but I get this error "String could not be parsed as XML". Unfortunately I can't work on the xml file, as it is downloaded from an external source, so I was wondering if there's any easy way to fix it.

Here's my code:

<?php
$requestAddress = "test.xml";

// Gets data
$xml_str = file_get_contents($requestAddress,0);

// Parses XML
$xml = new SimplexmlElement($xml_str);
?>

I have looked all around but I can't see any solution to the problem, or at least I don't understand them! :-)

don
  • 4,113
  • 13
  • 45
  • 70

2 Answers2

4

My guess is that file_get_contents() does not take into account the encoding of the file. Your accents might be considered as invalid characters and break the XML structure.

Use mb_detect_encoding() to detect the encoding.

Use utf8_encode() to convert your characters to UTF-8.

CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • You were right! Thank's sooooooo much! Now everything works just fine, no matter how many accents! – don Dec 06 '11 at 23:28
0

Be sure the encoding agrees with the header of the document (usually utf-8).

If the encoding (charset) does not match, you can load the file as a generic text file and then convert the character encoding with the iconv() function to the correct one.

jap1968
  • 7,747
  • 1
  • 30
  • 37