Is there any way to deal with XML responses for data

I have a major supplier who is using Xml in an api response, is there any way of working with this in Wappler or is there a way to convert the xml to json?

Help would be appreciated.

No, there is no way to deal with xml data sources in Wappler.
You need to convert it to JSON first.

Any suggestions or pointer on the best way to do this from the return information, so it can be used in Wappler ??

Perhaps following will help you: XML Imports

With some PHP knowledge you could use it to convert your XML to JSON.

Not the best with PHP @patrick.
Is this something that will be included in Wappler at some point (fingers crossed) ?

I think it would be a very useful feature to have this built into Wappler. I realise in many areas JSON has replaced XML, but XML is still widely used and probably will be for a long time (eg Amazon Marketplace).

2 Likes

Try with a PHP SC custom formatter:

function formatter_xml2json($val) {
    $xml = simplexml_load_string(strval($val));
    return json_decode(json_encode($xml));
}

And then just use

{{yourxmlresponse.data.xml2json()}}
1 Like

How would that work on a page that is returned by the supplier?
Sorry to ask but I’m not great with pho mate

Do you have the response in Wappler?

I am assuming you have created an API action in SC that is querying the endpoint. You are receiving an xml as a response.

So your API action “api1” is calling the endpoint. You set that API action data type as text.
In the next steps you can access the JSON as api1.data.xml2json()

You can set a value as jsonResponse = {{ api1.data.xml2json() }} for convenience.

Hi @JonL little confused on the

So your API action “api1” is calling the endpoint. You set that API action data type as text.
In the next steps you can access the JSON as api1.data.xml2json()

Where would I add that in on the SC ?
Yes I am getting a response from the first api.

The way their system works is that I need to get a token first so this returns:
(token) 1fb51ce8-4e11-4cea-a90b-7d3b33172557(/token)
(expiry)09:26 09-May-2020(/expiry)

the () are <> in the responce.
How do you set the PHP SC Custom formatter?
What I need to do then is another query for the stock including the token, this is where I am getting suck.
You help on this is really appreciated :slight_smile:

If you only need the token value converting to json seems overkill. Just use a formatter and extract the value with a regex pattern.

Here you have this dummy api endpoint.

http://www.mocky.io/v2/5eb4a0290e00006100081d0b

It will respond with:

<response>
<token>1fb51ce8-4e11-4cea-a90b-7d3b33172557</token>
</response>

Here you have the SC file

<?php
require('../../dmxConnectLib/dmxConnect.php');


$app = new \lib\App();

$app->define(<<<'JSON'
{
  "settings": {
    "options": {}
  },
  "meta": {
    "options": {}
  },
  "exec": {
    "steps": [
      {
        "name": "api1",
        "module": "api",
        "action": "send",
        "options": {
          "url": "http://www.mocky.io/v2/5eb4a0290e00006100081d0b",
          "dataType": "text"
        },
        "output": false
      },
      {
        "name": "token",
        "module": "core",
        "action": "setvalue",
        "options": {
          "value": "{{api1.data.iwantmytoken()}}"
        },
        "output": true
      }
    ]
  }
}
JSON
);
?>

And here you have the custom formatter that will extract your token.

Create a file named custom.php in dmxConnectLib/lib/formatters/ and copy this piece in it.

<?php

namespace lib\core;

function formatter_iwantmytoken($val) {
    preg_match('/<token>(.+?)<\/token>/',strval($val),$token);
    return $token[1];
}

When you run this SC you will have your token available in the wappler token variable.

image

Adapt as per your needs.

Word of caution: This approach is just to extract the token value. To convert a full xml file to json is best to use the parser as mentioned previously.

I’ll add also the xml2json formatter with a more complex xml response in case it’s eventually needed.

XML response:

https://www.w3schools.com/xml/plant_catalog.xml

PHP SC formatter:

<?php

namespace lib\core;

function formatter_xml2json($val) {
    $xml = simplexml_load_string(strval($val));
    return json_decode(json_encode($xml));
}

SC file

<?php
require('../../dmxConnectLib/dmxConnect.php');


$app = new \lib\App();

$app->define(<<<'JSON'
{
  "settings": {
    "options": {}
  },
  "meta": {
    "options": {}
  },
  "exec": {
    "steps": [
      {
        "name": "api1",
        "module": "api",
        "action": "send",
        "options": {
          "url": "https://www.w3schools.com/xml/plant_catalog.xml",
          "dataType": "text"
        },
        "output": false
      },
      {
        "name": "json",
        "module": "core",
        "action": "setvalue",
        "options": {
          "value": "{{api1.data.xml2json()}}"
        },
        "output": true
      }
    ]
  }
}
JSON
);
?>

Output

Cheers for that buddy, got the token working so I’ll try and work through it now and get the product query working.
Do you mind if I have any more questions or problem to ask for further help ?

1 Like

Sure! As long as I know the answer or it doesn’t take me 2 weeks to learn it :smiley:
Good luck!

1 Like

cheers buddy

Hey @JonL,

Thanks for sharing the xml to json code snippet, it works wonderful.

My question is related to how to configure the API schema in order to bind the data attribute to the font end.

The API I am using is returning XML so I added the set value step to convert the XML output to JSON, and I configured the API schema manually after that, but still I can not use the output of the JSON returned to bind data with the front end, do you have any solution for this?

1 Like

I am not at my computer at the moment so I can’t check myself.

You need to return the output. Try adding a response step with the contents of the variable that holds the json.