【问题标题】:How to convert XML result to JSON array or PHP array Using PHP Code如何使用 PHP 代码将 XML 结果转换为 JSON 数组或 PHP 数组
【发布时间】:2019-08-26 21:01:36
【问题描述】:

我想使用 PHP 5.6 将以下代码从 XML 转换为 PHP 数组,或从 XML 转换为 JSON 数组。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <readDataResponse xmlns="http://tempuri.org/">
      <readDataResult>
        <sampleItems>
          <SampleModel>
            <sampleId>1</sampleId>
            <firstName>Amran</firstName>
            <lastName>Aditya</lastName>
          </SampleModel>
          <SampleModel>
            <sampleId>2</sampleId>
            <firstName>Abeds</firstName>
            <lastName>Lukman</lastName>
          </SampleModel>
        </sampleItems>
      </readDataResult>
    </readDataResponse>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

  • 如果您包含代码示例并提出特定问题,我们可以尝试帮助解决任何错误。否则,这真的不是问题。
  • 你有链接教程如何使用php将上面的xml代码制作成json数组吗?
  • 我曾经尝试过这个教程,但我无法转换为 JSON 数组
  • PHP convert XML to JSON的可能重复

标签: php html arrays json xml


【解决方案1】:

XML 转 JSON:

{
    "Envelope": {
        "Body": {
            "readDataResponse": {
                "readDataResult": {
                    "sampleItems": {
                        "SampleModel": [
                            {
                                "sampleId": "1",
                                "firstName": "Amran",
                                "lastName": "Aditya"
                            },
                            {
                                "sampleId": "2",
                                "firstName": "Abeds",
                                "lastName": "Lukman"
                            }
                        ]
                    }
                },
                "_xmlns": "http://tempuri.org/"
            },
            "__prefix": "soap"
        },
        "_xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "__prefix": "soap"
    }
}

XML 到 PHP:

How to convert XML into array in PHP?

【讨论】:

  • 如何使用 PHP 实现它?请告诉我你的代码
  • XML 是我想要发布的全部内容。如果其他人想发布 PHP 代码,他也可以发布。
  • 你有教程吗?如何使用 php 代码将 xml 结果如上面的代码转换为 json 数组
  • 我将教程添加到我的答案中。
  • 我曾经尝试过.. 但我会再试一次所有的例子
【解决方案2】:

这里可以将xml字符串转成json JSON转数组

$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

【讨论】:

  • 好的。我会试试这个。谢谢
猜你喜欢
  • 2018-07-30
  • 2019-09-07
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2014-11-25
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多