简体   繁体   English

如何从PHP中的XML解析变量

[英]How to parse the variables from a xml in php

Suppose I get XML content from a URL in a PHP variable (eg $new ). 假设我从PHP变量(例如$new )中的URL获取XML内容。

Below is an example of this XML content: 下面是此XML内容的示例:

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

When I do the parsing of XML content in php like:- 当我在php中解析XML内容时:-

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

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

But I want to retrieve the inner content from the tag, eg retrieve the value of only abc OR xyz OR mno . 但是我想从标记中检索内部内容,例如,仅检索abc OR xyz OR mno

How can do that? 那怎么办

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

That's not an XML-String it's a JSON object. 那不是XML-String,而是JSON对象。 You should parse it with json_decode ! 您应该使用json_decode解析它! http://php.net/manual/de/function.json-decode.php http://php.net/manual/de/function.json-decode.php

Your XML markup contains JSON-formatted data. 您的XML标记包含JSON格式的数据。

You need to perform JSON parsing on this string to extract the desired values, using json_decode for instance: 您需要对此字符串执行JSON解析以提取所需的值,例如使用json_decode

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

I've used $xmlObj->getResult like in your answer and I assume you didn't post code with error. 我在您的答案中使用了$xmlObj->getResult ,并且我假设您没有发布带有错误的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM