无法使用 simplexml_load_string 读取 XML 文件
我一直在尝试读取 xml 文件,但它给了我一个奇怪的错误。 我的 XML 如下
<?xml version='1.0' encoding='UTF-8'?>
<response>
<url>http://xyz.com</url>
<token>xxxxxxx<token>
</response>
要阅读此内容,我正在使用它
simplexml_load_string(variable containing xml goes here)
,但它给了我这个错误
警告:simplexml_load_string() [function.simplexml-load-string]: 实体:第 1 行:解析器错误:需要开始标记,“<”未找到于 第 47 行
警告:simplexml_load_string() [function.simplexml-load-string]:1 在第 47 行
警告:simplexml_load_string() [function.simplexml-load-string]:^ 在第 47 行
I have been trying to read the xml file, but it is giving me a strange error.
My XML is as follows
<?xml version='1.0' encoding='UTF-8'?>
<response>
<url>http://xyz.com</url>
<token>xxxxxxx<token>
</response>
To read this I am using
simplexml_load_string(variable containing xml goes here)
but it is giving me this error
Warning: simplexml_load_string() [function.simplexml-load-string]:
Entity: line 1: parser error : Start tag expected, '<' not found in
on line 47Warning: simplexml_load_string() [function.simplexml-load-string]: 1
in on line 47Warning: simplexml_load_string() [function.simplexml-load-string]: ^
in on line 47
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢大家的关注和回复,
但问题出在其他地方
在我的curl请求中,我没有将CURLOPT_RETURNTRANSFER设置为true,这导致XML没有被记录在变量中,并且以某种方式打印在屏幕上,给人一种它来自变量的错觉,但事实并非如此。
但是我将 CURLOPT_RETURNTRANSFER 设置为 1,现在它给了我正确的结果。
很抱歉犯了愚蠢的错误。
谢谢大家。
Thanx all for the attention and replies,
But the problem lied somewhere else
In my curl request the i had not set CURLOPT_RETURNTRANSFER to true and that was causing the XML not to be recorded in the variable and was somehow was getting printed on the screen giving the illusion that it is coming from the variable but it was not.
However i set CURLOPT_RETURNTRANSFER to 1 and it is giving me correct results now.
Sorry for the silly mistake.
and thanx all.
您从 API 获得的响应不是格式良好/有效的 XML:
由于这是一个 API 响应,您需要先修复它,然后 simplexml 才能实际读取它。您可以利用 整洁的扩展文档解决这个问题:
这将产生以下 XML首先修复未闭合的标签,然后删除空的
token
元素:相关:
The response you get from the API is not well-formed/valid XML:
As this is an API response you need to fix it prior simplexml can actually read it in. You can make use of the tidy extension Docs to solve this:
This will produce the following XML by first fixing unclosed tags and then removing empty
token
elements:Related:
正如您正确指出的那样,CURLOPT_RETURNTRANSFER 设置为 1 非常重要!
As you rightly pointed out, CURLOPT_RETURNTRANSFER set to 1 is very important!