How do I tell DOMDocument->load() what encoding I want it to use?

前端 未结 3 1248
旧巷少年郎
旧巷少年郎 2021-01-04 10:48

I search for and process XML files from elsewhere, and need to transform them with some XSLTs. No problem. Using PHP5 and the DOM library, everything\'s a snap. Worked fine,

3条回答
  •  囚心锁ツ
    2021-01-04 11:25

    Does this work for you?

    $doc = new DOMDocument('1.0', 'iso-8859-1');
    $doc->load($xmlPath);
    

    Edit: Since it appears that this doesn't work, what you could do instead is similar to your existing method but without the temp file. Read the XML file from your source just using standard IO operations (file_get_contents() or something), then perform whatever changes to the encoding you need (iconv() or utf8_decode()) and then use loadXML()

    $myXMLString = file_get_contents($xmlPath);
    $myXMLString = utf8_decode($myXMLString);
    $doc = new DOMDocument('1.0', 'iso-8859-1');
    $doc->loadXML($myXMLString);
    

提交回复
热议问题