如何从php中的xml解析变量

时间:2015-08-31 08:45:23

标签: php xml

假设我从PHP变量中的URL获取XML内容(例如$new)。

以下是此XML内容的示例:

<getResult>
[{"abc": "123","xyz":"234","mno":"we4r5t"}]
</getResult>

当我在php中解析XML内容时,如: -

$xmlObj = simplexml_load_string($new);
error_log($xmlObj->getResult);

我得到[{"abc": "123","xyz":"234","mno":"we4r5t"}]

但我想从标签中检索内部内容,例如只检索abc OR xyzmno的值。

怎么做?

3 个答案:

答案 0 :(得分:0)

[{"abc": "123","xyz":"234","mno":"we4r5t"}]

这不是XML-String,而是JSON对象。你应该用json_decode解析它! http://php.net/manual/de/function.json-decode.php

答案 1 :(得分:0)

您的XML标记包含JSON格式的数据。

您需要对此字符串执行JSON解析以提取所需的值,例如使用json_decode

$json = json_decode($xmlObj);
$abc = $json[0]["abc"];

答案 2 :(得分:0)

$xmlObj = simplexml_load_string($new);
$json = json_decode($xmlObj->getResult);
error_log($json[0]['abc']); // 123

我在你的回答中使用了$xmlObj->getResult,我认为你没有发布错误的代码。