Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



10.6k

From here: http://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.



10.0k

From here: http://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.



49.7k
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: http://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.