Forum Discussion

tmarshall's avatar
tmarshall
Advisor
7 years ago

XML and JSON with #List

Hi, 

Working on a project to list out new posts in the last day to increment GSA (google search appliance), but they need the results to be in specific XML format and layout for the crawler to crawl.

 

I've been able to make up an endpoint a text/HTML and get the output HREF. 

 

 

<#assign datenow=.now?date?long>
<#-- days 1, hours 24, minutes 60, seconds 60, ms 1000 -->
<#assign daysinms = 1*24*60*60*1000>
<#assign datediff= datenow - daysinms>
<#assign x = rest("2.0","/search?q=" + "SELECT view_href, post_time, subject, board FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow} ORDER BY post_time ASC"?url) />
<#assign count= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow}"?url).data.count />
<#if count gt 0>
<#list x.data.items as message > 
<a href="${message.view_href}">${message.subject}</a></br>
</#list> 
<#else>
No posts in last 24 hours
</#if>

I've reviewed the the knowledge base and got the examples to work, from the following article. https://community.lithium.com/t5/Developer-Knowledge-Base/Return-FreeMarker-objects-as-JSON-or-XML-strings/ta-p/184087 

 

 

When I attempt to to try and make this into an XML output it fails, but am missing something. If I take out the 'list' section and put in text between the response success it displays.  

 

<#setting url_escaping_charset='UTF-8'>
<#assign datenow = .now?date?long />
<#assign daysinms = 1*24*60*60*1000 />
<#assign datediff = datenow - daysinms />
<#assign count= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow}"?url).data.count />
<#assign x = rest("2.0","/search?q=" + "SELECT view_href, post_time, subject, board FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow} ORDER BY post_time ASC"?url) />
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<#if count gt 0>
	<list x.data.items as message> 
		<response status="success">
			<message type="message" href="${message.view_href}"></message>
		</response>
	<#list> 
<#else>
	<response status="error">
		<error code="c001">
			<message>No posts in last 24 hours</message>
        </error>
     </response>
</#if>

 

 

The XML output should be something like the following, and list out all the URLs in the mime type from the list:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE gsafeed PUBLIC "-//Google//DTD GSA Feeds//EN">

<gsafeed>
<header>
<datasource>web</datasource>
<feedtype>incremental</feedtype>
</header>
<group>
<record mimetype="text/plain/" url="https://community.domain.com/t5/Adapters/FAQ-Help-me/m-p/1496618"/>
<record mimetype="text/plain/" url="https://community.domain.com/t5/Adapters/No-internet-Troubleshooting/m-p/1496619"/>
</group>
</gsafeed>

How can I go from what is working to an XML output? Appreciate the insight.

 

 

  • tmarshall

    You have some error in syntax error code.

    Please try this piece of code.

    One more thing YOU endpoint must be of View Content Type  : text/xml

     

    Please see this screenshot: https://prnt.sc/ib0po2

     

    You XML result should be like this : https://prnt.sc/ib0qko

     

     

    <#assign datenow=.now?date?long>
    <#assign daysinms = 1*24*60*60*1000>
    <#assign datediff= datenow - daysinms>
    <#assign x = rest("2.0","/search?q=" + "SELECT view_href, post_time, subject, board FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow} ORDER BY post_time ASC"?url) />
    <#assign count= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow}"?url).data.count />
    <#if count gt 0>
    <response status="success">
    <messages>
    <#list x.data.items as message>
    <message type="message" href="${message.view_href}"></message>
    </#list>
    </messages>
    </response>
    <#else>
    <response status="error">
    <error code="c001">
    <message>No posts in last 24 hours</message>
    </error>
    </response>
    </#if>

     

  • tmarshall

    You have some error in syntax error code.

    Please try this piece of code.

    One more thing YOU endpoint must be of View Content Type  : text/xml

     

    Please see this screenshot: https://prnt.sc/ib0po2

     

    You XML result should be like this : https://prnt.sc/ib0qko

     

     

    <#assign datenow=.now?date?long>
    <#assign daysinms = 1*24*60*60*1000>
    <#assign datediff= datenow - daysinms>
    <#assign x = rest("2.0","/search?q=" + "SELECT view_href, post_time, subject, board FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow} ORDER BY post_time ASC"?url) />
    <#assign count= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow}"?url).data.count />
    <#if count gt 0>
    <response status="success">
    <messages>
    <#list x.data.items as message>
    <message type="message" href="${message.view_href}"></message>
    </#list>
    </messages>
    </response>
    <#else>
    <response status="error">
    <error code="c001">
    <message>No posts in last 24 hours</message>
    </error>
    </response>
    </#if>

     

    • tmarshall's avatar
      tmarshall
      Advisor

      Excellent. I managed to figure out the rest ;) 

       

      <#assign datenow=.now?date?long>
      <#assign daysinms = 1*24*60*60*1000>
      <#assign datediff= datenow - daysinms>
      <#assign x = rest("2.0","/search?q=" + "SELECT view_href, post_time, subject, board FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow} ORDER BY post_time ASC"?url) />
      <#assign count= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE replies.count(*)=0 AND depth=0 AND post_time > ${datediff} AND post_time < ${datenow}"?url).data.count />
      <#if count gt 0>
      <response status="success">
      <gsafeed>
      <header>
      <datasource>web</datasource>
      <feedtype>incremental</feedtype>
      </header>
      <group>
      <#list x.data.items as message>
      <record mimetype="text/plain/" url="${message.view_href}"></record>
      </#list>
      </group>
      </gsafeed>
      </response>
      <#else>
      <response status="error">
      <error code="c001">
      <message>No posts in last 24 hours</message>
      </error>
      </response>
      </#if>