使用 simplexml_load_string 解析 XML 响应

发布于 2024-11-18 23:35:07 字数 721 浏览 3 评论 0原文

我正在使用 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)是因为,由于某种原因,未经编辑的响应使用 &lt; 符号而不是 '<' 打印。迹象。任何建议或建议都会非常有帮助!

顺便说一句,我正在打印这样的对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛时过境迁 2024-11-25 23:35:07

改为使用:

var_dump($xml);

它将显示变量的类型。这样您就可以知道变量是 false 还是 null,而 print_r 则不然。如果变量为 nullfalse 则不会打印任何内容。

因此,如果您获得 SimpleXML 对象,一切都很好,但是如果您获得 false ,则解析遇到错误。为了能够找出问题所在,您需要打开 E_WARNING,因为 simplexml_load_string 会生成警告而不是错误。

在此处了解更多内容

Use instead:

var_dump($xml);

It will show you what type the variable is. This way you will know if the variable is false or null, which print_r does not. If the variable is null or false it won't print anything.

So, If you get an SimpleXML object all is fine, if you however get false then the parsing encountered an error. To being able to figure out what went wrong you need to turn on E_WARNING, since simplexml_load_string produces a warning and not a error.

Read more here

缱绻入梦 2024-11-25 23:35:07

您可以将 xml 对象转换为数组,然后像数组一样使用。

示例:

      $xml = simplexml_load_file('path/to/xml');

        function toarray($data) {
        if (is_object($data)) $data = get_object_vars($data);
        return (is_array($data)) ? array_map(__FUNCTION__,$data) : $data;
        }

        $dat = toarray($xml);

现在 $dat 变成数组并像使用它一样

echo $dat['key_name'];

希望它可以帮助您获得结果。
谢谢
干杯...!!

You can convert your xml object into array and then use like an array.

Example :

      $xml = simplexml_load_file('path/to/xml');

        function toarray($data) {
        if (is_object($data)) $data = get_object_vars($data);
        return (is_array($data)) ? array_map(__FUNCTION__,$data) : $data;
        }

        $dat = toarray($xml);

Now $dat becomes array and use it like

echo $dat['key_name'];

Hope it may help to get your result.
Thank You
CHeers...!!

物价感观 2024-11-25 23:35:07

您可能正在寻找 asXML 函数。

SimpleXMLElement::asXML — 返回基于 SimpleXML 元素的格式正确的 XML 字符串

编辑

您在下面提到该对象从问题中发布的代码片段中返回空。你试过var_dump吗?我怀疑你实际上得到了“FALSE”的返回。这将打印为空,当您有格式错误的 XML 并且您尝试通过问题中的代码传递它时,就会发生这种情况。

您可以尝试以下几个步骤:

  • 在浏览器中按 ctrl+u - 我敢打赌您确实需要一个 &tl; ,其中您确实想要一个 << /代码>。或者,当您想要 " 时,您会看到一个 "。CTRL-U 应该会显示这一点。
  • 将浏览器的输出复制到本地文件中,看看会发生什么simplexml_load_file 说明了这一点,这可能是您缺少结束标记或其他内容。

You're probably looking for the asXML function.

SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element

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:

  • Hit ctrl+u in your browser -- I'll bet that you've a &tl; where you really want a <. Either that or you have an " when you want ". CTRL-U should show you that.
  • Copy the output from the browser into a local file and see what simplexml_load_file says about it. It could be that you're missing a closing tag or something.
早乙女 2024-11-25 23:35:07

它不使用“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文