使用 simplexml_load_string 解析 XML 响应
我正在使用 PHP 来解析 XML 响应,
经过一些调试后,我发现我的应用程序出现了奇怪的情况。这就是我一直在做的事情,我提出请求,然后回应整个响应。响应是预期的 XML。当我复制整个文档并将其粘贴到一个 php 脚本中,然后在其上运行 simplexml_load_string 时,它会像您所期望的那样输出一个 Simple XML 对象。
当我从实际请求运行它时,我打印响应,我可以看到它,看起来像普通的 XML。但是当我使用 simplexml_load_string 加载 xml 字符串时,它什么也不打印。我尝试了一些变体,但它返回的每个对象都是空的。该脚本正在运行,因为我在脚本末尾回显了“完成”。我已经尝试过其中的每一个:
$xml = simplexml_load_string(htmlspecialchars_decode($response_body));
$xml = simplexml_load_string($response_body);
$xml = new SimpleXMLElement($response_body);
我尝试过的第一个(使用 htmlspecialchars_decode)是因为,由于某种原因,未经编辑的响应使用 <
符号而不是 '<' 打印。迹象。任何建议或建议都会非常有帮助!
顺便说一句,我正在打印这样的对象:
print_r($xml);
I am using PHP to parse an XML response,
After some debugging, I have found a strange occurrence with my application. This is what I've been doing, I make the request and then I echo the entire response. The response is as-expected XML. When I copy this entire document and paste it into a php script which then runs simplexml_load_string on it, it spits out a Simple XML object like you would expect.
When I run it from the actual request, I print the response, I can see it, looks like normal XML. But then when I load the xml string using simplexml_load_string, it prints nothing at all. I have tried a few variations but every object it returns is just empty. The script is running because I echo "Done" at the end of the script. I have tried each of these:
$xml = simplexml_load_string(htmlspecialchars_decode($response_body));
$xml = simplexml_load_string($response_body);
$xml = new SimpleXMLElement($response_body);
The first one I've tried (with htmlspecialchars_decode) is because, for some reason, the unedited response, prints with <
signs instead of '<' signs. Any suggestions, or advice would be hugely helpful!
By the way, I am printing the object like this:
print_r($xml);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
改为使用:
它将显示变量的类型。这样您就可以知道变量是
false
还是null
,而print_r
则不然。如果变量为null
或false
则不会打印任何内容。因此,如果您获得
SimpleXML
对象,一切都很好,但是如果您获得false
,则解析遇到错误。为了能够找出问题所在,您需要打开E_WARNING
,因为simplexml_load_string
会生成警告而不是错误。在此处了解更多内容
Use instead:
It will show you what type the variable is. This way you will know if the variable is
false
ornull
, whichprint_r
does not. If the variable isnull
orfalse
it won't print anything.So, If you get an
SimpleXML
object all is fine, if you however getfalse
then the parsing encountered an error. To being able to figure out what went wrong you need to turn onE_WARNING
, sincesimplexml_load_string
produces a warning and not a error.Read more here
您可以将 xml 对象转换为数组,然后像数组一样使用。
示例:
现在
$dat
变成数组并像使用它一样希望它可以帮助您获得结果。
谢谢
干杯...!!
You can convert your xml object into array and then use like an array.
Example :
Now
$dat
becomes array and use it likeHope it may help to get your result.
Thank You
CHeers...!!
您可能正在寻找 asXML 函数。
编辑
您在下面提到该对象从问题中发布的代码片段中返回空。你试过var_dump吗?我怀疑你实际上得到了“FALSE”的返回。这将打印为空,当您有格式错误的 XML 并且您尝试通过问题中的代码传递它时,就会发生这种情况。
您可以尝试以下几个步骤:
&tl;
,其中您确实想要一个<< /代码>。或者,当您想要
"
时,您会看到一个"
。CTRL-U 应该会显示这一点。You're probably looking for the asXML function.
EDIT
You mentioned below that the object is returning empty from the pieces of code posted in the question. Have you tried var_dump? I suspect that you're actually getting the return of 'FALSE'. That will print out as nothing and it happens when you have malformed XML and you try to pass it through the code in your question.
Here are a couple of steps you can try:
&tl;
where you really want a<
. Either that or you have an"
when you want"
. CTRL-U should show you that.它不使用“echo”打印的原因是
echo
打印一个变量。simplexml_load_string 是一个内置函数,它将 XML 函数的输出转换为对象。
您不能使用
echo
来打印对象。print_r()
是打印对象的更好选择。The reason that it's not printing with 'echo', is that
echo
prints a variable.The simplexml_load_string is a built in function that turns an output of an XML function into an object.
You can not use
echo
to print an object.print_r()
is a better option to print objects.