Adding encoding to an xml file for SimpleXml?
I have an external xml file which I have to pick up, no encoding is set but I have discovered it it's payload is encoded ISO-8859-1.
I know this because if I manually edit the file to enc开发者_如何学Pythonoding="ISO-8859-1" then it is processed as expected.
Can I tell simplexml what encoding to deal with as I instantiate the simplexml object?
Addendum
Because the xml file was so dirty I might end up using xmllint - posting here for anyone else interested - format so it is indented, set encoding where it did not exist and clean up bad entities (& and so on)
xmllint --format --encode iso-8859-1 -o cleansed.xml dirty.xml
You can set the encoding for a DomDocument and then convert it to simplexml by using simplexml_import_dom():
$dom = new DomDocument('1.0', 'iso-8559-1');
$dom->load('externalfile.xml');
if (!$dom) {
echo 'Parsing error';
exit;
}
$s = simplexml_import_dom($dom);
精彩评论