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.

5
  • 1
    it "doesnt work" is never enough information. at least provide the error message. Commented Feb 15, 2012 at 8: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... Commented Feb 15, 2012 at 9:03
  • I modified my question with latest update. Please check. Commented Feb 15, 2012 at 9:17
  • possible duplicate of Input is not proper UTF-8 encoding Commented Feb 15, 2012 at 9: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. Commented Feb 15, 2012 at 10:32

5 Answers 5

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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.

Comments

1

if your xml is soap request. You can try this

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

Comments

-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.

3 Comments

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.
Strange using $xml = simplexml_load_string(file_get_contents('mediatheques-aphp.bibliomedias.net/12.xml')); works on my machine, no xml errors whatsoever.
Quoting the OP: I have to get this through Amazon S3 getObject().
-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);

1 Comment

This is the full version of what he's doing and it helps in no way. simplexml_load_string is bugged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.