cancel
Showing results for 
Search instead for 
Did you mean: 

freemarker xml encoding

abruzzi
Champ on-the-rise
Champ on-the-rise
I'm building a webscript that takes all nodes in a specified directory with a specific aspect applied, and outputs some XML with various bits of metadata, including comments.  I've got almost every thing working, but it seems the comments are all surrounded with an html paragraph tag.  That I don't care about, but the characters are not valid XML so I need to escape them.  Looking in freemarker docs it seems I just need to append a ?xml to the variable name like so: ${comments.content?xml} to make it work.  But it doesn't.  Am I missing something? 

thanks,

Geof

<strong>itprojects.get.desc.xml</strong>

<webscript>
    <shortname>Get all IT Projects</shortname>
    <description>Returns a list of active IT Projects</description>
    <url>/org/donaanacounty/itprojects?path={path}</url>
    <url>/org/donaanacounty/itprojects.xml?path={path}</url>
    <format default="xml">extension</format>
    <authentication runas="admin">none</authentication>
    <transaction>none</transaction>
</webscript>

<strong>itprojects.get.js</strong>

var path = args.path + "";

if (path.substring(path.length-1, path.length) != "/") {
    path = path + "/";
}

var projects = search.luceneSearch("PATH:\""+path+"*\" AND ASPECT:\"dac:itproject\"");


if (projects == null || projects.length == 0) {

   status.code = 404;
    status.message = "No projects found";
    status.redirect = true;

} else {

   var projectInfo = new Array();

   for (i=0; i < projects.length; i++){

        projectInfo[i] = {};

        /* dac:itproject properties */
        var nr = projects[i].nodeRef.toString().split('/');

        projectInfo[i].id = nr[nr.length - 1];
        projectInfo[i].title = projects[i].properties["dac:itproject-title"];
        projectInfo[i].inceptdate = projects[i].properties["dac:itproject-inceptdate"];
        projectInfo[i].completedate = projects[i].properties["dac:itproject-completedate"];

        /* dac:itproject associations */

        if (projects[i].assocs["dac:itproject-owner"] !== null) {
            projectInfo[i].owner = projects[i].assocs["dac:itproject-owner"][0].properties["cm:firstName"] + " " + projects[i].assocs["dac:itproject-owner"][0].properties["cm:lastName"];
        }

        if (projects[i].assocs["dac:itproject-itmanager"] !== null) {
            projectInfo[i].itmanager = projects[i].assocs["dac:itproject-itmanager"][0].properties["cm:firstName"] + " " + projects[i].assocs["dac:itproject-itmanager"][0].properties["cm:lastName"];
        }

        if (projects[i].properties["dac:itproject-department"] !== null){
            projectInfo[i].departments = projects[i].properties["dac:itproject-department"].split(',');
        }

        if (projects[i].tags !== null){
            projectInfo[i].tags = projects[i].tags;
        }

        if (projects[i].childAssocs["fm:discussion"] !== null){

            var comments = new Array();

            var discussion = projects[i].childAssocs["fm:discussion"][0].childAssocs["cm:contains"][0].children;

            for (j=0; j < discussion.length; j++){
                comments[j] = {};

                comments[j].creator = discussion[j].properties["cm:creator"];
                comments[j].created = discussion[j].properties["cm:created"];
                comments[j].modifier = discussion[j].properties["cm:modifier"];
                comments[j].modified = discussion[j].properties["cm:modified"];
                comments[j].content = discussion[j].content;
            }


            projectInfo[i].comments = comments;
        }

   }

   model.itprojects = projectInfo;

}

<strong>itprojects.get.xml.ftl</strong>

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:dac="http://www.donaanacounty.org/model/donaanacounty/1.0">
    <dac:projects>
    <#list itprojects as project>
        <dac:project id="${project.id}">
               <#if project.title??><dac:title>${project.title}</dac:title></#if>
               <#if project.inceptdate??><dac:inceptdate>${project.inceptdate?datetime?string("yyyy-MM-dd")}</dac:inceptdate></#if>
               <#if project.completedate??><dac:completedate>${project.completedate?datetime?string("yyyy-MM-dd")}</dac:completedate></#if>
               <#if project.owner??><dac:owner>${project.owner}</dac:owner></#if>
               <#if project.itmanager??><dac:itmanager>${project.itmanager}</dac:itmanager></#if>
        <#if project.departments??>
               <dac:departments>
                   <#list project.departments as department>
                        <dac:department>${department}</dac:department>
                   </#list>
              </dac:departments>
       </#if>


        <#if project.tags??>
               <dac:tags>
                   <#list project.tags as tag>
                        <dac:tag>${tag}</dac:tag>
                   </#list>
              </dac:tags>
       </#if>


        <#if project.comments??>
               <dac:comments>
                   <#list project.comments as comment>
                        <dac:comment creator="${comment.creator}" created="${comment.created?datetime?string("yyyy-MM-dd")}" modifier="${comment.modifier}" modified="${comment.modified?datetime?string("yyyy-MM-dd")}">
                            ${comment.content?xml}
                        </dac:comment>
                   </#list>
              </dac:comments>
       </#if>



        </dac:project>
    </#list>
    </dac:projects>
</root>
3 REPLIES 3

abruzzi
Champ on-the-rise
Champ on-the-rise
let me add the following sample output:



<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:dac="http://www.donaanacounty.org/model/donaanacounty/1.0">
    <dac:projects>
            <dac:project id="44081c20-7229-437b-866d-082d27b50570">
               <dac:title>abc</dac:title>
               <dac:inceptdate>2014-06-10</dac:inceptdate>
               <dac:completedate>2014-06-28</dac:completedate>
               <dac:owner>Alice Beecher</dac:owner>
               <dac:itmanager>Mike Jackson</dac:itmanager>
               <dac:departments>
                        <dac:department>BOCC</dac:department>
              </dac:departments>


               <dac:tags>
                        <dac:tag>evil</dac:tag>
                        <dac:tag>good</dac:tag>
              </dac:tags>


               <dac:comments>
                        <dac:comment creator="admin" created="2014-06-10" modifier="admin" modified="2014-06-10">
                            <p>this sucks</p>
                        </dac:comment>
                        <dac:comment creator="admin" created="2014-06-10" modifier="admin" modified="2014-06-10">
                            <p>this really sucks</p>
                        </dac:comment>
              </dac:comments>



        </dac:project>
    </dac:projects>
</root>

mitpatoliya
Star Collaborator
Star Collaborator
Try using CDATA. Surround your content with CDATA it escape all html string.

abruzzi
Champ on-the-rise
Champ on-the-rise
doh! never mind.  ?xml is working, my browser is post processing before displaying.