Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中解析xml中的变量_Php_Xml - Fatal编程技术网

如何在php中解析xml中的变量

如何在php中解析xml中的变量,php,xml,Php,Xml,假设我从PHP变量中的URL获取XML内容(例如$new) 以下是此XML内容的示例: <getResult> [{"abc": "123","xyz":"234","mno":"we4r5t"}] </getResult> 我得到了[{“abc”:“123”,“xyz”:“234”,“mno”:“we4r5t”}] 但是我想从标记中检索内部内容,例如仅检索abc或xyz或mno的值 你怎么能这么做 [{"abc": "123","xyz":"234","mno":"w

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

以下是此XML内容的示例:

<getResult>
[{"abc": "123","xyz":"234","mno":"we4r5t"}]
</getResult>
我得到了
[{“abc”:“123”,“xyz”:“234”,“mno”:“we4r5t”}]

但是我想从标记中检索内部内容,例如仅检索
abc
xyz
mno
的值

你怎么能这么做

[{"abc": "123","xyz":"234","mno":"we4r5t"}]
这不是XML字符串,而是JSON对象。你应该用json_decode解析它!
您的XML标记包含JSON格式的数据

您需要对该字符串执行JSON解析以提取所需的值,例如:

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

我在您的回答中使用了
$xmlObj->getResult
类似的方法,我假设您没有发布错误的代码。

内部内容的格式称为“JSON”。(这使得XML包装有些毫无意义。)
$json = json_decode($xmlObj);
$abc = $json[0]["abc"];
$xmlObj = simplexml_load_string($new);
$json = json_decode($xmlObj->getResult);
error_log($json[0]['abc']); // 123