PHP simplexml_load_string 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9290145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:30:06  来源:igfitidea点击:

PHP simplexml_load_string not working

phpxml

提问by Prasad Rajapaksha

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 echothe 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 $s3Dataexactly contain the XML content in the given link.

我有一个从 XML 文件中检索到的 XML 字符串。我无法直接使用simplexml_load_file(). 原因是我必须通过 Amazon S3 getObject() 获得它。当我echo输入字符串时,我可以看到输出是一个真正的 XML 文件。但是当我尝试使用波纹管代码将它加载到 XML 对象时,它不起作用。该变量$s3Data完全包含给定链接中的 XML 内容。

$xml = simplexml_load_string($s3Data);

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

我需要你的帮助来解决这个问题。我得到的 XML 字符串在这里可用。

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

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.

我创建了 XML 字符串并进行了测试。现在我收到以下错误。我该如何解决这些问题。

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

警告:simplexml_load_string() [function.simplexml-load-string]:实体:第 12 行:解析器错误:输入的 UTF-8 不正确,指示编码!字节:C:\Program Files\Zend\Apache2\htdocs\voxmedia\application\modules\import\controllers\NaiveController.php 中的 0xEF 0x76 0x65 0x3C 第 1458 行

警告: 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

警告: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.

谢谢。

回答by vitaly goji

simplexml_load_stringand simplexml_load_fileare 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 DOMDocumentinstead (worked like a charm).

simplexml_load_string并且simplexml_load_file有问题并且在带有复杂标记的 XML 上不能很好地工作。我尝试了这两个函数来解析一些 VALID XML SOAP 响应,甚至无法获得错误消息。我不得不DOMDocument改用(像魅力一样工作)。

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

这是我们假设检查 simplexml 是否有错误的方法(如果您很幸运看到错误,而不仅仅是从函数返回的“false”)

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

foreach( libxml_get_errors() as $error ) {

    print_r($error);

}

回答by Prasad Rajapaksha

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

我可以通过一个小的解决方法找到我的问题的答案。simplexml_load_string()连续显示编码错误。所以我用作simplexml_load_file()波纹管。我已将文件保存到本地位置,然后加载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.

但是我仍然不明白为什么simplexml_load_string()不能解析相同的东西。

回答by Bob Fanger

When you echoa 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.

当你echo的变量实际上是一个对象时,它的__toString() 方法被调用以将对象转换为字符串,但是当你将对象传递给函数时,对象不会被转换。

Use:

用:

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

To cast the $s3Data object to a string.

将 $s3Data 对象转换为字符串。

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

PS:使用 var_dump($s3Data) 和 var_dump($xml) 来调试变量的内容而不是 echo。

回答by Visakh B Sujathan

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