Retrieve data from XML endpoint
I have custom XML endpoint which looks like this:
<levels>
<level>
<name>LEVEL 1, NAME1</name>
<points>0</points>
</level>
<level>
<name>LEVEL 2, NAME2</name>
<points>10</points>
</level>
</levels>
Now I would like to get this data in custom component. How can I do that? I tried to use rest call but I am not sure which url should I use.
My endpoint url looks like this:
https://communityurl/plugins/custom/communityname/communityname/ranking_levels
In that case, I would probably just add the details (as json, xml or whatever structure you want to use) in one of the custom content fields (/t5/bizapps/bizappspage/tab/community%3Aadmin%3Acontent%3Acustom-content) which is not used and get the content from the custom component with rest (https://community.lithium.com/t5/Developer-Documentation/bd-p/dev-doc-portal?section=commv1&leaf-id=Settings.name.name#Settings.name.name )
For ex. for custom content 9:
<#assign custom_content_9 = restadmin('/settings/name/customcontent.9_text').value />
omygoodness- As ChiaraS said, custom content is the best way to do this, I would suggest you to use JSON structure for this. You also can create macro to store your XML or JSON structure in a freemarker variable.
Thus you can include your macro in any custom content and endpoint to access your XML or JSON structure.Do you mean something like this?
<#assign custom_content_9 = restadmin('/settings/name/customcontent.9_text').value />
<#assign test = custom_content_9?eval>
${test}
And how should I format JSON to be correct?
For now my custom_content_9 looks exactly like this and ?eval didn't help:
{
"levels": [
{
"name": "LEVEL 1",
"points": "0"
},
{
"name": "LEVEL 2",
"points": "10"
}]
}Error:
FreeMarker template error (HTML_DEBUG mode; use RETHROW in production!)
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to an extended_hash (wrapper: f.c.HashLiteral$SequenceHash): ==> test [in template "ranking-test.ftl" at line 5, column 3] ---- FTL stack trace ("~" means nesting-related): - Failed at: ${test} [in template "ranking-test.ftl" at line 5, column 1]
SOLUTION
Ok. I think I have solution :) Custom content with JSON or rather array should look like this:
{ "name": "LEVEL1", "points": "0", "name": "LEVEL2", "points": "10", "name": "LEVEL3", "points": "50" }
Then we have ?keys for keys in my case "name" and "points" and ?values for values like "LEVEL1" and number of points
<#assign test = restadmin('/settings/name/customcontent.9_text').value?eval> <#list test?keys as k> ${k} </#list> <#list test?values as v> ${v} </#list>
So in the end ?eval worked for me but proper format of data is crucial here.