简体   繁体   中英

PHP simplexml_load_string not working

I have a XML String retrieved from a XML file. I can't access the file directly using simplexml_load_file() . Reason is I have to get this through Amazon S3 getObject(). When I echo the string I can see the out put as a real XML file. But when I try to load it to a XML object using bellow code it doesn't work. The variable $s3Data exactly contain the XML content in the given link.

$xml = simplexml_load_string($s3Data);

I need your help to figure this out. The XML string I am getting is available here.

http://mediatheques-aphp.bibliomedias.net/12.xml

EDIT

I created the XML string and tested. Now I get following errors. How can I fix these.

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 12: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xEF 0x76 0x65 0x3C in C:\\Program Files\\Zend\\Apache2\\htdocs\\voxmedia\\application\\modules\\import\\controllers\\NaiveController.php on line 1458

Warning: simplexml_load_string() [function.simplexml-load-string]: 2009 naïve in C:\\Program Files\\Zend\\Apache2\\htdocs\\voxmedia\\application\\modules\\import\\controllers\\NaiveController.php on line 1458

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\\Program Files\\Zend\\Apache2\\htdocs\\voxmedia\\application\\modules\\import\\controllers\\NaiveController.php on line 1458

OK Now I feel the issue is with sort of string encoding or something. Is there any way to fix this?

Thanks.

simplexml_load_string and simplexml_load_file are buggy and don't work well on XML with a complex markup. I tried both functions to parse some VALID XML SOAP responses and could not even get an error message. I had to use DOMDocument instead (worked like a charm).

Here is how we suppose to check simplexml for errors (if you are lucky to see errors, not just "false" returned from function)

libxml_use_internal_errors(true);
$xml=simplexml_load_string($myXMLData); //or simplexml_load_file

foreach( libxml_get_errors() as $error ) {

    print_r($error);

}

I could find an answer to my question with a small workaround. simplexml_load_string() continuously displayed encoding errors. So I used simplexml_load_file() as bellow. I've saved the file in to local location and then load with simplexml_load_file() .

$tempLocalPath = "temp/data.xml";
file_put_contents($tempLocalPath, $s3Data);
$xml = simplexml_load_file($tempLocalPath);

However still I don't understan why simplexml_load_string() couldn't parse the same thing.

You should try this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
simplexml_load_string($result);

When you echo a variable which is actually an object its __toString () method is called to convert the object to a string but when you pass the object to function the object issn't converted.

Use:

$xml = simplexml_load_string( (string)$s3Data);

To cast the $s3Data object to a string.

PS: Use var_dump($s3Data) and var_dump($xml) to debug the contents of a variable instead of echo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM