While XML sometimes seems to be something like a relic of the past in REST communication, there are still quite a few REST resources that respond to requests only with XML. So we have had a few PocketQuery customers asking us to implement XML support for PocketQuery REST datasources.
In PocketQuery 3.12.0, we now provide a solution: a built-in XML-to-JSON conversion for responses from REST datasources. PocketQuery will detect if your response is in XML format and go ahead with this transformation.
For example, assume this is your XML response:
Your auto-converted result will be:
Analysis & Processing the Result with a Converter
Without further work, PocketQuery will now display an error. Note that I also checked the "Debug" macro parameter to get some more information:
In the screenshot, we can first see a ClassCastException and the message "String cannot be cast to List". (Note that while I'm writing this I notice we should really work on some more user-friendly error-reporting. But that's another topic.) This error occurs because the response is not in the format expected by PocketQuery.
You can also see an info box titled "Raw result retrieved from the datasource" that shows the raw XML string and you can see a "Result passed to the converter" info box. Here you can see the input and the output of the XML auto-conversion: PocketQuery detected that the response is an XML string and converted it to a JSON string. So what now?
If you have used PocketQuery for a while, you'll know that we have a solution for such "unexpected format" problems in PocketQuery: Converters! For the above example, we could create a converter like this:
The key point here is: the actual projects array will be accessible in the result with the key `resultObject.Projects.Project`. This is because the XML result was converted to this structure:
We are using a Java library called JSON-Java for this conversion. A more detailed article about this library can be found here. Another example of how this conversion takes place can be found here. In general, I think it makes most sense to use the "Debug" parameter in the PocketQuery macro and then see what the raw XML result was and to what JSON it was converted. That will give you a hint on how to write a converter for it. If you need help, please don't hesitate to create a support request.
The result with the above converter will be this:
We wish you a happy XML'ing with PocketQuery!