Reputation: 4532
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! :-)
Upvotes: 2
Views: 2804
Reputation: 5377
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.
Upvotes: 4
Reputation: 7763
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.
Upvotes: 0