Source: site.view [edit]
Function name: toXML
Arguments: wrapper,data,options
Description: Converts a data object into an XML string. Wrapper object will produce top level with fields as attributes, containing data object split using elements.
Page type: webl
Render function:  
Module: siteutil

Page source:

var res = "";
var wrapperLabel = nil;
var usecdata = (options.cdata ? "false") == "true";

// wrapper = [. label="xml", version="1.0" .];
// data = [. a=[. a1="abc", a2=1 .], b=[. b1="adfd" .] .];

// Format outside wrapper. Wrapper can be a label or an object.
// If an object, wrapper will use the "label" field to state the name
// of the element and put the other fields as attributes.
var Header = fun(wrapper)
   var res = "";
   if wrapper != nil then
      if Stringp(wrapper) then
         wrapperLabel = ToString(wrapper);
         res = res + "<" + wrapperLabel + ">\n"
      elsif Objectp(wrapper) then
         wrapperLabel = (wrapper.label? "xml");
         res = res + "<" + wrapperLabel;
         every field in ToList(wrapper) do
            if ToString(field) != "label" then
               res = res + " " + ToString(field) + `="` + (ToString(wrapper[field]) ? "true") + `"`;
            end
         end;
         res = res + ">\n"
      end
   end;
   res
end;

var GetXMLValue;

var Body = fun(spc, data)
   var res = "";
   if Objectp(data) then
      every field in ToList(data) do
         res = res + spc + "<" + ToString(field) + ">" + 
                     GetXMLValue(spc + "  ", data[field]);
         if Objectp(data[field]) or Listp(data[field]) then
            res = res + spc
         end;
         res = res + spc + "</" + ToString(field) + ">\n"
      end
   elsif Listp(data) then
      every field in data do
         if Objectp(field) then
            var label = (field.label ? "labelMissing");
            var value = (field.value ? "valueMissing");
            res = res + spc + "<" + ToString(label) + ">" + 
                     GetXMLValue(spc + "  ", value);
            if Objectp(value) or Listp(value) then
               res = res + spc
            end;
            res = res + "</" + ToString(label) + ">\n"
         end
      end
   end;
   res
end;


GetXMLValue = fun(spc, value)
   var res = "";
   if Objectp(value) or Listp(value) then
      res = "\n" + Body(spc, value)
   else
      if usecdata then
         res = "<![CDATA[" + ToString(value) + "]]>"
       else
         res = ToString(value)
       end
   end;
   res
end;

res = Header(wrapper);
res = res + Body("  ", data);

if wrapperLabel != nil then
   res = res + "</" + wrapperLabel + ">\n"
end;

res;