0

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
  • 1
    it "doesnt work" is never enough information. at least provide the error message. – Gordon Feb 15 '12 at 08:52
  • Hi. There is no error message. My code work with other XML String. Please don't think about a file. Just think that you have a php string variable which has the content of given URL. `$s3Data` is a string variable which contains things in the given XML file. Don't think about getObject() etc... – Prasad Rajapaksha Feb 15 '12 at 09:03
  • I modified my question with latest update. Please check. – Prasad Rajapaksha Feb 15 '12 at 09:17
  • possible duplicate of [Input is not proper UTF-8 encoding](http://stackoverflow.com/questions/2507608/error-input-is-not-proper-utf-8-indicate-encoding-using-phps-simplexml-lo) – Gordon Feb 15 '12 at 09:19
  • It's looks like possible duplicate. But their answers `utf8_encode() didn't work for me. I've put my own answer with the way I fixed it. Thanks Gordon for your help anyway. – Prasad Rajapaksha Feb 15 '12 at 10:32

5 Answers5

5

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);

}
vitaly goji
  • 256
  • 4
  • 13
  • Oh goodness gracious! I've been trying for hours to load the result of a SOAP request string with `simplexml_load_string` and it insisted on just not working (without any error). Thanks for the `DOMDocument` reference, made my day! – Luís Cruz Jul 17 '15 at 08:52
2

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.

Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
0

if your xml is soap request. You can try this

$testxml= htmlentities(strval($xml));
 
-1

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);
xskxzr
  • 12,442
  • 12
  • 37
  • 77
Visakh B Sujathan
  • 229
  • 1
  • 4
  • 25
-1

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.

Bob Fanger
  • 28,949
  • 7
  • 62
  • 78
  • No casting didn't work. I tried that. My code work with other XML String. Please don't think about a file. Just think that you have a php string variable which has the content of given file. – Prasad Rajapaksha Feb 15 '12 at 09:00
  • Strange using $xml = simplexml_load_string(file_get_contents('http://mediatheques-aphp.bibliomedias.net/12.xml')); works on my machine, no xml errors whatsoever. – Bob Fanger Feb 15 '12 at 09:09
  • Quoting the OP: *I have to get this through Amazon S3 getObject().* – Gordon Feb 15 '12 at 09:11