Jump to content

UTF-16 & XML Error


bushkilldogo

Recommended Posts

Hello,

 

I'm running into a problem with UTF-16 encoded XML and SimpleXMLElement when trying to read from an incoming email. Works fine with UTF-8 encoded XML and I can even make it work by forwarding the same 16 message to myself (which I think converts it back to 8).

 

Please help this is driving me nuts!

 

 

THE ERROR...

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : String not started expecting ' or " in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version=3D"1.0" encoding=3D"utf-16"?>=0D=0A<?adf version=3D"1.0"?>= in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Malformed declaration expecting version in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version=3D"1.0" encoding=3D"utf-16"?>=0D=0A<?adf version=3D"1.0"?>= in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Blank needed here in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version=3D"1.0" encoding=3D"utf-16"?>=0D=0A<?adf version=3D"1.0"?>= in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : parsing XML declaration: '?>' expected in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version=3D"1.0" encoding=3D"utf-16"?>=0D=0A<?adf version=3D"1.0"?>= in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version=3D"1.0" encoding=3D"utf-16"?>=0D=0A<?adf version=3D"1.0"?>= in [MY FILE] on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in [MY FILE] on line 4

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in [MY FILE]:4 Stack trace: #0 [MY FILE](4): SimpleXMLElement->__construct('<?xml version=3...') #1 [MY FILE](59): include('/nfs/c05/h03/mn...') #2[MY FILE](44): include('/nfs/c05/h03/mn...') #3 {main} thrown in [MY FILE] on line 4

 

 

THE XML...

<?xml version="1.0" encoding="utf-16"?>
<?adf version="1.0"?>
<adf>
        <prospect>
                <id sequence="1" source="Urban Science">1828605</id>
                <requestdate>2010-04-09T12:04:47-04:00</requestdate>
                <vehicle interest="buy">
                        <year>2010</year>
                        <make>Subaru</make>
                        <model>Tribeca</model>
                        <trim>Limited</trim>
                </vehicle>
                <customer>
                        <contact>
                                <name part="first">Carlos</name>
                                <name part="last">Diaz</name>
                                <email preferredcontact="1">[email protected]</email>
                                <phone preferredcontact="1">123456789</phone>
                                <address>
                                        <street line="1">123 Some Street</street>
                                        <city>Flemington</city>
                                        <regioncode>NJ</regioncode>
                                        <postalcode>08822</postalcode>
                                </address>
                        </contact>
                        <comments></comments>
                </customer>
                <vendor>
                        <id sequence="1" source="Subaru Dealer Code">020184</id>
                </vendor>
                <provider>
                        <id sequence="1" source="Subaru Brand Site" />
                        <name>Subaru Brand Site</name>
                        <service>Request Dealer Quote</service>
                </provider>
        </prospect>
</adf>

 

THE PHP...

$body = 	imap_fetchbody($imap, $i, getPartNumber($struct, "XML"));	// get text part number
$adf = 	new SimpleXMLElement($body); 

 

 

Link to comment
https://forums.phpfreaks.com/topic/198154-utf-16-xml-error/
Share on other sites

Well it seems that after playing around and looking at the raw code i came up with a work around.

 

The message comes in with hex characters such as...

 

=3D

=0D

=0A

 

after playing and playing and playing i came up with a solution where I have essentially stripped all those characters before processing.

 

$body =	trim($body);
$body =	ereg_replace("\n", '', $body);
$body =	ereg_replace("\r", '', $body);
$body = 	str_replace("= ", " ", $body);
$body = 	ereg_replace("%0D%0A", " ", $body);
$body = 	ereg_replace("=0D=0A", "", $body);
$body = 	str_replace('=3D"', '="', $body);
$body = 	str_replace('"=20', '" ', $body);

 

Now granted it is not the prettiest but it works. I will however take any suggestions on improving this.

 

Thanks.  :D

 

 

Link to comment
https://forums.phpfreaks.com/topic/198154-utf-16-xml-error/#findComment-1039745
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.