SimpleXMLElement returns empty object resolved

simple xml elemet empty object

Hello buddy, I hope you are doing well in this article we will learn about how we can convert XML to JSON, I know when you are trying to convert XML to JSON you get an empty object.

But we have the solution, on how to convert XML to JSON via PHP. Look for the below XML response.

<?xml version="1.0" encoding="UTF-8"?>
<sEnvelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <sHeader>
      <aAction s:mustUnderstand="1">http://tempuri.org/IServiceFinderQuery/GetServicesforPincodeResponse</aAction>
   </sHeader>
   <sBody>
      <GetServicesforPincodeResponse xmlns="http://tempuri.org/">
         <GetServicesforPincodeResult xmlns:b="http://schemas.datacontract.org/2004/07/SAPI.Entities.Finder" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <bAirValueLimit>200000</bAirValueLimit>
            <bAirValueLimiteTailPrePaid>1000000</bAirValueLimiteTailPrePaid>
            <bApexEDLAddDays>0</bApexEDLAddDays>
            <bApexEDLDist>0</bApexEDLDist>
            <bApexETailTDD10Inbound>No</bApexETailTDD10Inbound>
            <bApexETailTDD10Outbound>No</bApexETailTDD10Outbound>
            <bApexETailTDD12Inbound>No</bApexETailTDD12Inbound>
            <bApexETailTDD12Outbound>No</bApexETailTDD12Outbound>
            <bApexEconomyInbound>No</bApexEconomyInbound>
            <bApexEconomyOutbound>No</bApexEconomyOutbound>
            <bApexEtailRVP>Full RVP</bApexEtailRVP>
            <bApexInbound>Yes</bApexInbound>
            <bApexOutbound>Yes</bApexOutbound>
            <bApexTDD>No Srv.</bApexTDD>
            <bAreaCode>PAT</bAreaCode>
            <bBlueDartHolidays>
               <bHoliday>
                  <bDescription>SUNDAY</bDescription>
                  <bHolidayDate>2021-12-26T00:00:00</bHolidayDate>
               </bHoliday>
               <bHoliday>
                  <bDescription>SUNDAY</bDescription>
                  <bHolidayDate>2022-01-02T00:00:00</bHolidayDate>
               </bHoliday>
            </bBlueDartHolidays>
            <bDPDutsValueLimit>500000</bDPDutsValueLimit>
            <bDPTDD10Inbound>No</bDPTDD10Inbound>
            <bDPTDD10Outbound>No</bDPTDD10Outbound>
            <bDPTDD12Inbound>No</bDPTDD12Inbound>
            <bDPTDD12Outbound>No</bDPTDD12Outbound>
            <bDartPlusRVP>Full RVP</bDartPlusRVP>
            <bDomesticPriorityInbound>Yes</bDomesticPriorityInbound>
            <bDomesticPriorityOutbound>Yes</bDomesticPriorityOutbound>
            <bDomesticPriorityTDD>No</bDomesticPriorityTDD>
            <bEDLAddDays>0</bEDLAddDays>
            <bEDLDist>0</bEDLDist>
            <bEDLProduct />
            <bEmbargo>No</bEmbargo>
            <bErrorMessage>Valid</bErrorMessage>
            <bGroundEDLAddDays>0</bGroundEDLAddDays>
            <bGroundEDLDist>0</bGroundEDLDist>
            <bGroundInbound>Yes</bGroundInbound>
            <bGroundOutbound>Yes</bGroundOutbound>
            <bGroundRVP>Full RVP</bGroundRVP>
            <bGroundValueLimit>49999</bGroundValueLimit>
            <bGroundValueLimiteTailPrePaid>49999</bGroundValueLimiteTailPrePaid>
            <bIsError>false</bIsError>
            <bPinCode>800008</bPinCode>
            <bPincodeDescription>PATNA CITY</bPincodeDescription>
            <bRVPEmbargo>Full RVP</bRVPEmbargo>
            <bServiceCenterCode>PXX</bServiceCenterCode>
            <beTailCODAirInbound>Yes</beTailCODAirInbound>
            <beTailCODAirOutbound>Yes</beTailCODAirOutbound>
            <beTailCODGroundInbound>Yes</beTailCODGroundInbound>
            <beTailCODGroundOutbound>Yes</beTailCODGroundOutbound>
            <beTailExpressCODAirInbound>Yes</beTailExpressCODAirInbound>
            <beTailExpressCODAirOutbound>Yes</beTailExpressCODAirOutbound>
            <beTailExpressPrePaidAirInbound>Yes</beTailExpressPrePaidAirInbound>
            <beTailExpressPrePaidAirOutound>Yes</beTailExpressPrePaidAirOutound>
            <beTailPrePaidAirInbound>Yes</beTailPrePaidAirInbound>
            <beTailPrePaidAirOutound>Yes</beTailPrePaidAirOutound>
            <beTailPrePaidGroundInbound>Yes</beTailPrePaidGroundInbound>
            <beTailPrePaidGroundOutbound>Yes</beTailPrePaidGroundOutbound>
         </GetServicesforPincodeResult>
      </GetServicesforPincodeResponse>
   </sBody>
</sEnvelope>

and if you try to convert the above XML into JSON by using the SimpleXML element definitely you will get an empty object because it’s not a SimpleXML element. It’s quite different from the simpler one.

We will do fewer tweaks to get our result in the JSON format look for the below code.

$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);
            $xml = new SimpleXMLElement($response);
            $body = $xml->xpath('//sBody');
            $array = json_encode((array)$body); 

the above code will give you below JSON format

[
   {
      "GetServicesforPincodeResponse": {
         "GetServicesforPincodeResult": {
            "bAirValueLimit": "200000",
            "bAirValueLimiteTailPrePaid": "1000000",
            "bApexEDLAddDays": "0",
            "bApexEDLDist": "0",
            "bApexETailTDD10Inbound": "No",
            "bApexETailTDD10Outbound": "No",
            "bApexETailTDD12Inbound": "No",
            "bApexETailTDD12Outbound": "No",
            "bApexEconomyInbound": "No",
            "bApexEconomyOutbound": "No",
            "bApexEtailRVP": "Full RVP",
            "bApexInbound": "Yes",
            "bApexOutbound": "Yes",
            "bApexTDD": "No Srv. ",
            "bAreaCode": "PAT",
            "bBlueDartHolidays": {
               "bHoliday": [
                  {
                     "bDescription": "SUNDAY",
                     "bHolidayDate": "2021-12-26T00:00:00"
                  },
                  {
                     "bDescription": "SUNDAY",
                     "bHolidayDate": "2022-01-02T00:00:00"
                  }
               ]
            },
            "bDPDutsValueLimit": "500000",
            "bDPTDD10Inbound": "No",
            "bDPTDD10Outbound": "No",
            "bDPTDD12Inbound": "No",
            "bDPTDD12Outbound": "No",
            "bDartPlusRVP": "Full RVP",
            "bDomesticPriorityInbound": "Yes",
            "bDomesticPriorityOutbound": "Yes",
            "bDomesticPriorityTDD": "No",
            "bEDLAddDays": "0",
            "bEDLDist": "0",
            "bEDLProduct": {},
            "bEmbargo": "No",
            "bErrorMessage": "Valid",
            "bGroundEDLAddDays": "0",
            "bGroundEDLDist": "0",
            "bGroundInbound": "Yes",
            "bGroundOutbound": "Yes",
            "bGroundRVP": "Full RVP",
            "bGroundValueLimit": "49999",
            "bGroundValueLimiteTailPrePaid": "49999",
            "bIsError": "false",
            "bPinCode": "800008",
            "bPincodeDescription": "PATNA CITY",
            "bRVPEmbargo": "Full RVP",
            "bServiceCenterCode": "PXX",
            "beTailCODAirInbound": "Yes",
            "beTailCODAirOutbound": "Yes",
            "beTailCODGroundInbound": "Yes",
            "beTailCODGroundOutbound": "Yes",
            "beTailExpressCODAirInbound": "Yes",
            "beTailExpressCODAirOutbound": "Yes",
            "beTailExpressPrePaidAirInbound": "Yes",
            "beTailExpressPrePaidAirOutound": "Yes",
            "beTailPrePaidAirInbound": "Yes",
            "beTailPrePaidAirOutound": "Yes",
            "beTailPrePaidGroundInbound": "Yes",
            "beTailPrePaidGroundOutbound": "Yes"
         }
      }
   }
]

If you want to convert XML to Array then follow the below code, it will help you to convert your XML result into a array formatted result.

$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);
            $xml = new SimpleXMLElement($response);
            $body = $xml->xpath('//sBody');
            $array = json_decode(json_encode((array)$body), TRUE); 
            dd($array);

The above code will give you the result in below format.

array:1 [
  0 => array:1 [
    "GetServicesforPincodeResponse" => array:1 [
      "GetServicesforPincodeResult" => array:54 [
        "bAirValueLimit" => "200000"
        "bAirValueLimiteTailPrePaid" => "1000000"
        "bApexEDLAddDays" => "0"
        "bApexEDLDist" => "0"
        "bApexETailTDD10Inbound" => "No"
        "bApexETailTDD10Outbound" => "No"
        "bApexETailTDD12Inbound" => "No"
        "bApexETailTDD12Outbound" => "No"
        "bApexEconomyInbound" => "No"
        "bApexEconomyOutbound" => "No"
        "bApexEtailRVP" => "Full RVP"
        "bApexInbound" => "Yes"
        "bApexOutbound" => "Yes"
        "bApexTDD" => "No Srv. "
        "bAreaCode" => "PAT"
        "bBlueDartHolidays" => array:1 [
          "bHoliday" => array:2 [
            0 => array:2 [
              "bDescription" => "SUNDAY"
              "bHolidayDate" => "2021-12-26T00:00:00"
            ]
            1 => array:2 [
              "bDescription" => "SUNDAY"
              "bHolidayDate" => "2022-01-02T00:00:00"
            ]
          ]
        ]
        "bDPDutsValueLimit" => "500000"
        "bDPTDD10Inbound" => "No"
        "bDPTDD10Outbound" => "No"
        "bDPTDD12Inbound" => "No"
        "bDPTDD12Outbound" => "No"
        "bDartPlusRVP" => "Full RVP"
        "bDomesticPriorityInbound" => "Yes"
        "bDomesticPriorityOutbound" => "Yes"
        "bDomesticPriorityTDD" => "No"
        "bEDLAddDays" => "0"
        "bEDLDist" => "0"
        "bEDLProduct" => []
        "bEmbargo" => "No"
        "bErrorMessage" => "Valid"
        "bGroundEDLAddDays" => "0"
        "bGroundEDLDist" => "0"
        "bGroundInbound" => "Yes"
        "bGroundOutbound" => "Yes"
        "bGroundRVP" => "Full RVP"
        "bGroundValueLimit" => "49999"
        "bGroundValueLimiteTailPrePaid" => "49999"
        "bIsError" => "false"
        "bPinCode" => "800008"
        "bPincodeDescription" => "PATNA CITY"
        "bRVPEmbargo" => "Full RVP"
        "bServiceCenterCode" => "PXX"
        "beTailCODAirInbound" => "Yes"
        "beTailCODAirOutbound" => "Yes"
        "beTailCODGroundInbound" => "Yes"
        "beTailCODGroundOutbound" => "Yes"
        "beTailExpressCODAirInbound" => "Yes"
        "beTailExpressCODAirOutbound" => "Yes"
        "beTailExpressPrePaidAirInbound" => "Yes"
        "beTailExpressPrePaidAirOutound" => "Yes"
        "beTailPrePaidAirInbound" => "Yes"
        "beTailPrePaidAirOutound" => "Yes"
        "beTailPrePaidGroundInbound" => "Yes"
        "beTailPrePaidGroundOutbound" => "Yes"
      ]
    ]
  ]
]

I hope your issue has been resolved related to getting simple XMLElement getting an empty object, Please rate 5 if you are happy with my code.

4.5 2 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Spread the love
Subscribe
Notify of
guest

1 Comment
oldest
newest most voted
Inline Feedbacks
View all comments
Kristian
Kristian
9 months ago

You saved my life! Thank buddy.

1
0
Would love your thoughts, please comment.x
()
x