We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

API output XML to JSON and contrariwise

Hello ppl,

Is there any way to parse XML to JSON and contrariwise in Phalcon. Situation is the, I building a REST API (Back - end Phalcon) that provide data with JSON to my Front - end (AngularJS). So my rest api should be designed to retrieve data from an SOAP (XML) services.. and pass it to my front end in JSON .. Any sugestions? Thanks



17.5k

From here: https://stackoverflow.com/questions/8830599/php-convert-xml-to-json

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

Why use XML at all though? Are you getting the XML data from somewhere else? I would design the API to send JSON and ditch XML altogether.



23.6k

From here: https://stackoverflow.com/questions/8830599/php-convert-xml-to-json

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

Why use XML at all though? Are you getting the XML data from somewhere else? I would design the API to send JSON and ditch XML altogether.

Yep, I getting data in XML from an external API.



79.0k
Accepted
answer
edited Dec '15

That wouldn't work with SOAP. With plain XML yes, but not with SOAP. Simplexml just cannot handle SOAP header's etc. First you need to manually remove SOAP elements and get the clean XML message. Then to validate XML wth simplexml and so on. I can provide an example tomorrow. It's proven solution in a production environment.

From here: https://stackoverflow.com/questions/8830599/php-convert-xml-to-json

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

Why use XML at all though? Are you getting the XML data from somewhere else? I would design the API to send JSON and ditch XML altogether.