API conversion from XML to Json

Would be great if there was a simpler way to convert api XML responses to json.
A lot of people even though it is outdated are still using XML.
We know you cleaver guys a Wappler would be able to nut this one out and make it happen.

Did you try the solution I posted?

PHP SC formatter:

<?php

namespace lib\core;

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

It’s pretty simple.

{{ api1.data.xml2json() }}

Yeah it did work, cheers for that. Biggest issue I’m having is with <@attributes> just can not get them to show. In the form assuming it’s something to do with the @

Can you give me a working example? I want to take a look. I suspect the person who designed that xml should probably be behind bars.

You shouldn’t start the name of an xml element with “@”

https://www.w3.org/TR/xml/#NT-NameStartChar

So if the XML is not valid there is nothing that Wappler can or should do to support that. However you would still be left with the custom formatter option to extract the value of that attribute.

But first we need to call the XML police. :smiley:

This is an example of what I am working with.

“data”: “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n 1\n 1\n <product item_code=“CZ51522MTAC”>\n CZ 515 22WMR Tactical 10rnd TB\n <extended_description><p>The CZ 515 is an Australian exclusive bolt action lever release rimfire rifle designed for hunters and target shooters.</p>\r\n<p>The rifle is fitted with an automatic bolt stop in the rear position and an ergonomically designed lever for releasing the bolt. The lever release is ergonomically designed and can be transferred to the left hand side of the firearm to accommodate left handed shooters<br /><br /> This version is a modern Tactical model with pistol grip, adjustable stock (length and cheek piece), picatinny rail, full length barrel shroud and supplied with a 10 round polymer magazine.<br /> Available in this 22LR as well as <a href=“https://www.winchesteraustralia.com.au/products/CZ51522MTAC”>22WMR</a>.<br /> <br /> The CZ 515 is also available in a stylish Hunter model with an American style beechwood stock and supplied with a 10 round polymer magazine.<br />Available in&nbsp;<a href=“https://www.winchesteraustralia.com.au/products/CZ51522AMERICAN”>22LR</a> and <a href=“https://www.winchesteraustralia.com.au/products/CZ51522MAMERICAN”>22WMR</a>.</p></extended_description>\n \n <image filename=“CZ51522MTAC.tag.1.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524568/attachments/Product/13668/CZ51522MTAC.tag.1.png\n \n <image filename=“CZ51522MTAC.tag.2.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524576/attachments/Product/13668/CZ51522MTAC.tag.2.png\n \n <image filename=“CZ51522MTAC.tag.0.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524587/attachments/Product/13668/CZ51522MTAC.tag.0.png\n \n <image filename=“CZ51522MTAC.tag.6.png”>\n <content_type>image/png</content_type>\n <updated_at>10:30 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524606/attachments/Product/13668/CZ51522MTAC.tag.6.png\n \n <image filename=“CZ51522MTAC.tag.4.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524751/attachments/Product/13668/CZ51522MTAC.tag.4.png\n \n <image filename=“CZ51522MTAC.tag.3.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524752/attachments/Product/13668/CZ51522MTAC.tag.3.png\n \n <image filename=“CZ51522MTAC.tag.5.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524758/attachments/Product/13668/CZ51522MTAC.tag.5.png\n \n \n <stock_level>-219.0</stock_level>\n \n\n”

That’s not a proper XML.
It seems at first sight some kind of html scraper that tries to build an xml file with the contents of an html page. But it’s mixing html and xml markup thus not being a valid xml.

This is a direct link which does look a little different
https://www.winchesteraustralia.com.au/dealer/api/v2/products?item_code=CZ51522MTAC&token=a6e6f781-6766-4939-ab87-aaa562c56700

Not sure why the @ shows as it does not on this one.

I could also do with a way to remove the href links out of the text, if that is possible.

That XML response is a mess. I would get in touch with them and let them know.

They need to remove all html tags. They shouldn’t be there.

Specifically from the extended_description and other attributes they may have with html elements included.

<extended_description>
<p>The CZ 515 is an Australian exclusive bolt action lever release rimfire rifle designed for hunters and target shooters.</p> <p>The rifle is fitted with an automatic bolt stop in the rear position and an ergonomically designed lever for releasing the bolt. The lever release is ergonomically designed and can be transferred to the left hand side of the firearm to accommodate left handed shooters<br /><br /> This version is a modern Tactical model with pistol grip, adjustable stock (length and cheek piece), picatinny rail, full length barrel shroud and supplied with a 10 round polymer magazine.<br /> Available in this 22LR as well as <a href="https://www.winchesteraustralia.com.au/products/CZ51522MTAC">22WMR</a>.<br /> <br /> The CZ 515 is also available in a stylish Hunter model with an American style beechwood stock and supplied with a 10 round polymer magazine.<br />Available in&nbsp;<a href="https://www.winchesteraustralia.com.au/products/CZ51522AMERICAN">22LR</a> and <a href="https://www.winchesteraustralia.com.au/products/CZ51522MAMERICAN">22WMR</a>.</p>
</extended_description>

I am afraid that if I create a custom formatter to parse for you this specific response it will not work with the next one and we will never finish.