0

I have this xml response that i want to covert to associative array and i have tried many solutions but nothing worked out.

<EXPEDICIONES NUM="1">
    <EXPEDICION>
        <PORTES>Pagados</PORTES>
        <DESCRIPCION_CLIENTE_INGLES>ENTREGA EFECTUADA</DESCRIPCION_CLIENTE_INGLES>
        <CARACTER_TEL>-</CARACTER_TEL>
        <PESO>2.6</PESO>
        <ENTREGA_SABADO>N</ENTREGA_SABADO>
        <DEBIDO>0</DEBIDO>
        <VALOR_TEL>-</VALOR_TEL>
        <RECOGIDA_SABADO>N</RECOGIDA_SABADO>
        <REMITE_PISO>-</REMITE_PISO>
        <REMITE_PROVINCIA>LISBOA</REMITE_PROVINCIA>
        <DESTINA_TIPO_NUM>IN</DESTINA_TIPO_NUM>
        <DESTINA_PUERTA>-</DESTINA_PUERTA>
        <PAGADO>3.38</PAGADO>
        <EXISTEN_APUNTES></EXISTEN_APUNTES>
        <DESTINA_PISO>-</DESTINA_PISO>
        <DESTINA_CCC_ID>-</DESTINA_CCC_ID>
        <FAMILIA>715338323</FAMILIA>
    </EXPEDICION>
</EXPEDICIONES>

Here is my code which is generating

$soap_request = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://consultaExpediciones.servicios.webseur">
   <soapenv:Header/>
   <soapenv:Body>
      <con:consultaExpedicionesStr>
         <con:in0>S</con:in0>
         <con:in1></con:in1>
         <con:in2></con:in2>
         <con:in3></con:in3>
         <con:in4>14097-83</con:in4>
         <con:in5>03-01-2016</con:in5>
         <con:in6>05-12-2017</con:in6>
         <con:in7></con:in7>
         <con:in8></con:in8>
         <con:in9></con:in9>
         <con:in10>83080189516234</con:in10>
         <con:in11>0</con:in11>
         <con:in12>velvet83</con:in12>
         <con:in13>velvet83</con:in13>
         <con:in14>N</con:in14>
      </con:consultaExpedicionesStr>
   </soapenv:Body>
</soapenv:Envelope>>';
$header = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "Content-length: " . strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "https://ws.seur.com/webseur/services/WSConsultaExpediciones");
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);

$result = (curl_exec($soap_do));

echo "<pre>";
print_r($result);
echo "</pre>";
die();

I have tried solutions like using SimpleXMLElement class and then json encode the xml but it did not worked.

$xml = new SimpleXMLElement($xmlString);

$json = json_encode($xml);   
$array = json_decode($json,TRUE);
3
  • So you tried "many solutions"... May we ask what those are? Because I cannot see any attempt you made in your question... Commented Jan 10, 2017 at 17:03
  • There are many questions regarding xml2array and i have tried most of them. Maybe i am having issues that i am getting xml response from soap call using curl. I tried both answered solutions and they did not worked. Commented Jan 10, 2017 at 17:20
  • "did not work" does not help. Post the code you tried and explain what exactly it is that is wrong in your opinion. Otherwise this looks like you are simply looking for someone else to do your work for you... Commented Jan 10, 2017 at 17:21

2 Answers 2

1

Quite simple using PHPs SimpleXML extension.

Read your XML (which returns an object)

$xml = new SimpleXMLElement($xmlString);

and since you want an associative array, just encode and decode it with json

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

http://php.net/manual/en/book.simplexml.php

Sign up to request clarification or add additional context in comments.

6 Comments

$xml = new SimpleXMLElement($result); $json = json_encode($xml); $array = json_decode($json, TRUE); echo "<pre>"; print_r($array); echo "</pre>"; It returned an empty array.
It does not have to be an associative array an index array can also solve the problem.
@HabibJutt If you could provide some of your code I could help you a bit better. Where do you get your XML data from?
I am getting XML data from a soap call using CURL.
@HabibJutt Could you edit your question and insert the appropriate code? Because if I insert your XML into my snippet it works well. Are you sure you are getting the correct data from your CURL request?
|
0

Using SimpleXml Library

$xml = new SimpleXMLElement($yourXmlString);

if($xml)
{
  $arrayDef = array();
  foreach ($xml->EXPEDICION as $item) {
     $arrayTemp = array();
     $arrayTemp['portes'] = (string)$item->PORTES;
     $arrayTemp['peso'] = (string)$item->PESO;

     array_push($arrayDef, $arrayTemp);
  }

  echo '<pre>' . print_r($arrayDef, true) . '</pre>';
}

$arrayDef will be your final array

1 Comment

Sorry but it did not worked. $xml = new SimpleXMLElement($result); if ($xml) { $arrayDef = array(); foreach ($xml->EXPEDICION as $item) { $arrayTemp = array(); $arrayTemp['portes'] = $item->PORTES; $arrayTemp['peso'] = $item->PESO; //etc... array_push($arrayDef, $arrayTemp); } } echo "<pre>"; print_r($arrayDef); echo "</pre>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.