Usability - Productivity - Business - The web - Singapore & Twins

XML Helper class for XAgents

When rendering your own output in XPages agent style (a.k.a XAgents) you might want to render XML as output. While you easily can try String concatenations to get your output, you will very fast hit the wall once you deal with special characters and double byte. One way to render proper XML is building a DOM Document. This can get very memory intensive if you have a large output. The alternative is to use the SAX Parser as I recommended before. The only catch is that you have to take care of XML nesting yourself. Since I use that quite often I created a little helper class long ago, that makes XML output a snap. Here is a sample XAgent code:
var exCon = facesContext. getExternalContext ( ) ;
var response = exCon. getResponse ( ) ;
var out :javax. servlet. ServletOutputStream = response. getOutputStream ( ) ;
var d :biz. taoconsulting. xmltools. SimpleXMLDoc d = new biz. taoconsulting. xmltools. SimpleXMLDoc ( ) ;
d. setOut (out ) ;
// Top level
d. openTag ( "Demo" ) ;
// Inside Demo
d. addSimpleTag ( "Client" , "ACME Inc" ) ;
d. addSimpleTag ( "Location" , "RoadRunner Valley" ) ;
// And done
d. closeDocument ( ) ;
resulting in this XML send to the caller:
<?xml version="1.0" encoding="UTF-8"?>
  <Client>ACME Inc </Client>
  <Location>RoadRunner Valley </Location>
The helper class has methods for: Opening Tags, simple Tags, empty Tags, CDATA content and closing a specific number of tags. It supports DocTypes and a stylesheet instruction but currently no namespaces. Have fun with it.
As usual: YMMV

Posted by on 18 October 2011 | Comments (2) | categories: XPages


  1. posted by Mark Hughes on Wednesday 19 October 2011 AD:
    will it convert special characters to xml compatible string?
  2. posted by Stephan H. Wissel on Thursday 20 October 2011 AD:
    yes it does. In UTF-8 you can have double byte, but greater and less need to be converted. And you don't need to worry about what rules exactly apply, the SAX Writer takes care of that.