When parsing XML in PHP recently, an error was thrown:
"Warning: Domdocument::load (): Input is not proper UTF-8, indicate encoding! BYTES:0XBB 0xb6 0xd3 0xAD in File:/e:/phpwork/shopproject/sendcms.xml, line:19 ine:\phpwork\shopproject\xml.php on line 4"
This means that DOMDocument's load function was in error at load, and then I looked at my XML file as follows
1 <?XML version= "1.0"?>2 < Package>3 <Header>4 <Roadno>12345</Roadno>5 <DataType>48</DataType>6 <Moduleno>12345</Moduleno>7 <SourceIP>127.0.0.1</SourceIP>8 <Sourceport>11000</Sourceport>9 <UserID>96998</UserID>Ten <Recordtime>2014-11-19 08:08:08:111</Recordtime> One <Imperative>1</Imperative> A <ResultFlag= "-1"MSG=" " /> - </Header> - <devicelist> the <DevitemDeviceID= "5100001"/> - <DevitemDeviceID= "5100002"/> - </devicelist> - <Visiondata> + <VisionitemTextInfo= "Welcome Entry"Picno=""FontName= "Song Body"FontSize= "Ten"FontColor= "Yellow"Fontformat= "Centered"Interval= "+"Effect= "1" /> - <VisionitemTextInfo= "slow slowing down."Picno=""FontName= "Song Body"FontSize= "Ten"FontColor= "Yellow"Fontformat= "Centered"Interval= "+"Effect= "1" /> + </Visiondata> A </ Package>
Sendcms.xml
And then found that the type definition does not define encoding, here I guess because there is a Chinese character, so the default is Utf-8 encoding, and then I find the method on the Internet, indeed, in stack over flow found a method, respectively, as follows
Method 1:
Solution: Read the text content, and then encode the text content to convert, using the Iconv function to convert, the specific code is as follows
//Read File contents$myfile=fopen("Sendcms.xml", ' R ');$content=fread($myfile,filesize("Sendcms.xml"));fclose($myfile);$content=Iconv(' UTF-8 ', ' Utf-8//ignore ',$content);$xmldoc-LoadXML ($content );Print $xmldoc->savexml ();
Method 1, using the Iconv function
Overall, this solution is good and can be used.
Original address: Stack OverFlow
PHP load XML encoding error, "Error:input is not proper UTF-8, indicate encoding! ”