0

I want to convert the below code from XML to PHP array using PHP 5.6 or from XML to a JSON array.

<?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>
Ones
  • 1
  • 2
  • If you include samples of your code and ask a specific question, we can try to help resolve any errors. Otherwise, this really isn't a question. – elbrant Apr 05 '19 at 02:04
  • May you have a link tutorial how to make this xml code above to json array Using php ? – Ones Apr 05 '19 at 02:09
  • I ever try this tutorial,, but I can't convert to JSON array – Ones Apr 05 '19 at 02:32
  • Possible duplicate of [PHP convert XML to JSON](https://stackoverflow.com/questions/8830599/php-convert-xml-to-json) – Anandhukrishna VR Apr 05 '19 at 04:13

2 Answers2

0

XML to 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 to PHP:

How to convert XML into array in PHP?

karel
  • 5,489
  • 46
  • 45
  • 50
0

Here you can convert xml string to json JSON to array

$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55